mrkrd / matlab_wrapper

Easy to use MATLAB wrapper for Python
GNU General Public License v3.0
78 stars 23 forks source link

Partly working Mac Version #1

Closed grahamj1978 closed 10 years ago

grahamj1978 commented 10 years ago

I have attempted to modify your code to work on my Mac running OS X 10.9.3 and Matlab 2014a. I have gotten it mostly working via the following changes:

1) Added 'Darwin' platform check to "load_engine_and_libs" function:

elif system == 'Darwin': if bits == '64bit': lib_dir = join(matlab_root, "bin", "maci64") else: unsopported_paltform(system,bits)
libeng = Library( join(lib_dir, 'libeng.dylib') ) libmx = Library( join(lib_dir, 'libmx.dylib') )
command = "{executable} {options}".format( executable=join(matlab_root, 'bin', 'matlab'), options=options )

2) Added the following PATH modifications: export PATH=/Applications/Matlab_R2014a.app/bin:$PATH export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Matlab2014a.app/bin/maci64:$DYLD_FALLBACK_LIBRARY_PATH

Upon testing the results of these changes I was able to start a session and perform many basic commands, however, the 'get' command, despite working once, thereafter gave me the following error (associated with line 187 in matlab_session.py):

matlab.put('a',23) matlab.eval('b=a*2') b=matlab.get('b') Error using save Can't write file stdio.

^CTraceback (most recent call last): File "", line 1, in File "build/bdist.macosx-10.9-intel/egg/matlab_wrapper/matlab_session.py", line 187, in get File "build/bdist.macosx-10.9-intel/egg/matlab_wrapper/matlab_session.py", line 230, in error_check

Hopefully, the issue can be identified and corrected, because so far, this is the closest I have gotten to a working Python/Matlab interface. (I tried pymatlab, but it caused python to crash.) Unfortunately, I'm now at a loss, and not knowledgeable enough to do much beyond what I already have.

mrkrd commented 10 years ago

Hi, thank you for your message.

I have attempted to modify your code to work on my Mac running OS X 10.9.3 and Matlab 2014a. I have gotten it mostly working via the following changes:

1) Added 'Darwin' platform check to "load_engine_and_libs" function: elif system == 'Darwin': if bits == '64bit': lib_dir = join(matlab_root, "bin", "maci64") else: unsopported_paltform(system,bits)

    libeng = Library(
        join(lib_dir, 'libeng.dylib')
    )
    libmx = Library(
        join(lib_dir, 'libmx.dylib')
    )

    command = "{executable} {options}".format(
        executable=join(matlab_root, 'bin', 'matlab'),
        options=options
    )

I have just committed your code to support OSX [1]. Could you test, if the code works?

2) Added the following PATH modifications: export PATH=/Applications/Matlab_R2014a.app/bin:$PATH export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Matlab2014a.app/bin/maci64:$DYLD_FALLBACK_LIBRARY_PATH

I think, I'll leave it out of the code now. Usually changing environment variables such as PATH, should be done by the user or installation program. I'm not sure about DYLD_FALLBACK_LIBRARY_PATH thou.

Upon testing the results of these changes I was able to start a session and perform many basic commands, however, the 'get' command, despite working once, thereafter gave me the following error (associated with line 187 in matlab_session.py):

matlab.put('a',23) matlab.eval('b=a*2') b=matlab.get('b') Error using save Can't write file stdio.

^CTraceback (most recent call last): File "", line 1, in File "build/bdist.macosx-10.9-intel/egg/matlab_wrapper/matlab_session.py", line 187, in get File "build/bdist.macosx-10.9-intel/egg/matlab_wrapper/matlab_session.py", line 230, in error_check

I have the same issue on Linux with MATLAB 2014a (other versions or Windows are unaffected). I think it's MATLAB's fault, because it's also present in engdemo.c (official demo for libeng).

The workaround is to get arrays of the type double/float only. This one works on Linux with 2014a:

matlab.put('a', 23.)
matlab.eval('b=a*2')
b = matlab.get('b')

Note, that 23. is a float in my snippet, therefore b will be a float too. In your example, a is an int and b becomes an int too (?).

Hopefully, the issue can be identified and corrected, because so far, this is the closest I have gotten to a working Python/Matlab interface. (I tried pymatlab, but it caused python to crash.) Unfortunately, I'm now at a loss, and not knowledgeable enough to do much beyond what I already have.

You can also try other versions of MATLAB. I hope, it will be fixed in 2014b.

Cheers Marek

[1] https://github.com/mrkrd/matlab_wrapper/commit/b4d2ea48ed269d1f76d0ef95257563810c73284d

grahamj1978 commented 10 years ago

Thanks for the update and information. I will go ahead and try your updated code, and see how forcing the variables to be floating point / double works. I'll also see if I can try an older version of Matlab.

In regards to the DYLD_FALLBACK_LIBRARY_PATH I had to set it so that Python (I presume) could properly load the libraries. If I did not do so, they failed to load. Also, I used the DYLD_FALLBACK_LIBRARY_PATH as opposed to DYLD_LIBRARY_PATH since the system will only attempt the fallback path when it can't find a library on the main path. From what I read doing this can sometimes avoid problems.

grahamj1978 commented 10 years ago

Okay, I have tested the updated code and it seems to work fine as long as I make sure to use floating point variables. I will try more thorough testing later.

mrkrd commented 10 years ago

grahamj1978 notifications@github.com writes:

Okay, I have tested the updated code and it seems to work fine as long as I make sure to use floating point variables. I will try more thorough testing later.

Great!

If you have a chance to test matlab_wrapper with other versions of MATLAB, just drop me a line about the results. I would like to update README about supported platforms and versions.