Giter Site home page Giter Site logo

matlab_wrapper's People

Contributors

andrescodas avatar schutte avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

matlab_wrapper's Issues

problem with eval in matlab_wrapper and python

Hi!
I am running the following code:
`def emin_epmgp(m,K,eta):'

global mtb    
if mtb == None:
    import matlab_wrapper
    mtb = matlab_wrapper.MatlabSession(matlab_root = "/Applications/MATLAB_R2014b.app")

try:
    mtb.put('K',0.5*(K+K.T))  # symetrize to avoid numerical inconsitencies
    mtb.put('m',m)
    mtb.put('eta',eta)
    mtb.eval("[e_min,Int_y,Probs_y,Int_eta] = emin_epmgp(m,K,eta)")
    e_min = mtb.get('e_min')
except:
    mtb.put('K',0.5*(K+K.T)+0.1*np.diag(np.ones(K.shape[0])))  # regularize in case of errors caused by K being singular
    mtb.put('m',m)
    mtb.put('eta',eta)
    mtb.eval("[e_min,Int_y,Probs_y,Int_eta] = emin_epmgp(m,K,eta)")
    e_min = mtb.get('e_min')
return e_min`

however, after running the following line:
mtb.eval("[e_min,Int_y,Probs_y,Int_eta] = emin_epmgp(m,K,eta)")

I am facing with the following error:
`RuntimeError Traceback (most recent call last)
in ()
----> 1 mtb.eval("[e_min,Int_y,Probs_y,Int_eta] = emin_epmgp(m,K,eta)")

/Users/ali/anaconda/lib/python2.7/site-packages/matlab_wrapper-0.9.5-py2.7.egg/matlab_wrapper/matlab_session.pyc in eval(self, expression)
172
173 ### Check for exceptions in MATLAB
--> 174 mxresult = self.libeng.engGetVariable(self.ep, 'ERRSTR')
175
176 error_string = self._libmx.mxArrayToString(mxresult)

/Users/ali/anaconda/lib/python2.7/site-packages/matlab_wrapper-0.9.5-py2.7.egg/matlab_wrapper/matlab_session.pyc in error_check(result, func, arguments)
236 func=str(func),
237 result=str(result),
--> 238 arguments=str(arguments)
239 ))
240 return result

RuntimeError: MATLAB function <FuncPtr object at 0x10f5ef7a0> failed (<matlab_wrapper.matlab_session.LP_mxArray object at 0x10a16e200>) with arguments:
(<matlab_wrapper.matlab_session.LP_Engine object at 0x10a16e290>, 'ERRSTR
_')`

I really do not have idea to fix it. Clearly, the problem come from eval, but I am not able to debug it.

PS: the issue is similar to this issue:
#11
But I could not find a solution.

Thanks for your time and consideration.

TypeError: a bytes-like object is required, not 'str'

I am faced with an unexpected error at the very beginning of this package.

The details of my environment are like this.
OS: Windows 10 64bit
Matlab R2014a
Python 3.6.0

whole source code I used is below

import matlab_wrapper
matlab = matlab_wrapper.MatalbSession()

Here is the error message

Traceback (most recent call last):
File "D:\research\sasaki\Script\Python\windows\hello.py", line 2, in
matlab = matlab_wrapper.MatlabSession()
File "C:\Users\CiNet\AppData\Roaming\Python\Python36\site-packages\matlab_wrapper\matlab_session.py", line 113, in init
engine, libeng, libmx, version = load_engine_and_libs(matlab_root, options)
File "C:\Users\CiNet\AppData\Roaming\Python\Python36\site-packages\matlab_wrapper\matlab_session.py", line 348, in load_engine_and_libs
version = tuple([int(v) for v in version_str.split('.')[:2]])
TypeError: a bytes-like object is required, not 'str'

Thank you in advance

not working for MATLAB 2015b, Mac OSX 10.10.5

All tests pass.

When I run eval_m_file.py:

Traceback (most recent call last):
  File "eval_m_file.py", line 25, in <module>
    main()
  File "eval_m_file.py", line 18, in main
    matlab.eval("run(my_script.m)")
  File "build/bdist.macosx-10.5-x86_64/egg/matlab_wrapper/matlab_session.py", line 177, in eval
  File "build/bdist.macosx-10.5-x86_64/egg/matlab_wrapper/matlab_session.py", line 241, in error_check
RuntimeError: MATLAB function <_FuncPtr object at 0x101f62c80> failed (<matlab_wrapper.matlab_session.LP_mxArray object at 0x101f84f80>) with arguments:
(<matlab_wrapper.matlab_session.LP_Engine object at 0x101f84dd0>, 'ERRSTR__')

Let me know what other info you need..

MatlabSession execption

Exception TypeError: 'isinstance() arg 2 must be a class, type, or tuple of classes and types' in <bound method MatlabSession.__del__ of <MatlabSession:/usr/local/MATLAB/R2015a>> ignored

Tests fail on master with MATLAB R2014a on Fedora 20

I don't think this is related to the engGetVariable issue (but maybe it is). All tests fail with

E   RuntimeError: MATLAB function <_FuncPtr object at 0x288e940> failed (<matlab_wrapper.matlab_session.LP_mxArray object at 0x2b1e9e0>) with arguments:
E   (<matlab_wrapper.matlab_session.LP_Engine object at 0x28db3b0>, 'df')

Also, I noticed that after calling matlab_wrapper.MatlabSession(), the function returns without an error but there is no MATLAB process started on my machine. Is this normal?

I'm running Fedora 20 x86_64, using the current master branch.

chararrays are all mixed up

Matlab functions returning a chararray are not properly parsed by the wrapper, they get all mixed up (all characters of 1st column, then all characters from 2nd column etc. in a single string, whereas it should return a row-wise list).

Example:

mlab.eval('res = ls')
mlab.get('res')

Returns:

'..777AAABCCCCCDDFHHIIILLLMMMMMMMMMMMMMOOOOOOOOOOOPPPPPPPPPPPPQQQQQQQQQQQQQQQQQQQQQSSSSSSTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTWWXaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccddddddddddddddddddddddddddddddddddddddddddddddddddddddeeeeeeeeeeeefffffffffffffggggggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiijjjjjjjjjjjjkkklllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll

Etc (this is an excerpt).

There is a way to workaround this by converting the output to a cellarray first using cellstr:

mlab.eval('res = cellstr(ls)')
mlab.get('res')

Launching multiple instances of MATLAB

I have a script that once launched in MATLAB runs in single core, so the idea would be to launch multiple instances (on different inputs) to take advantage of parallelism.

However, it seems by default this library will connect to the same MATLAB instance that was spawned by the earliest launch.

Is it possible to configure the module to launch a different instance of MATLAB?

python does not wait for matlab to finish

If I put, eval, get in a script and run it, it fails

  File "~/anaconda/lib/python2.7/site-packages/matlab_wrapper/matlab_session.py", line 174, in eval
    mxresult = self._libeng.engGetVariable(self._ep, 'ERRSTR__')

  File "~/anaconda/lib/python2.7/site-packages/matlab_wrapper/matlab_session.py", line 238, in error_check
    arguments=str(arguments)

RuntimeError: MATLAB function <_FuncPtr object at 0x112378530> failed (<matlab_wrapper.matlab_session.LP_mxArray object at 0x11242d7a0>) with arguments:
(<matlab_wrapper.matlab_session.LP_Engine object at 0x10ec60f80>, 'ERRSTR__')

while if I run the commands one-by-one in the console, it works.

I believe that the eval part takes time, and that the subsequent get fails because the variable does not exists yet.

It could be handled by matlab_wrapper, or perhaps by something like subprocess.wait()

That might be a bug or a feature. What's your take on it?

PS: my particular eval calls a function that does image processing. It does take a long second or so.

how to read a detector or .m function from matlab?

As I could not find a clear documentation on funcation and method for matlab_wrapper.
firstly I want to ask where can I find a tutorial or at leat list of functions for this library. I wonder what I could do with that exactly?
secondly is this Eval method for reading a function from matlab or just arrays?what is the exact input argument for matlab.Eval()??
last question is if I want to read a function(.m file) from matlab which is working with a detector(struct array), what method matlab_wrapper offers here?

Thank you in advance,

Is that suitable to call some self-write matlab function in a real-time python environment?

I need to write a real-time optical flow demo, and since I've already realized the optical flow algorithm I need in Matlab, I'm wondering if I can call the function in python to make the real-time demo.
And, the problem is I found the interface you created need to create a command window every time you call the Matlab function, which would cost too much time for a real-time task.
Is there any possibility I can finish my task?
Thank you.

Partly working Mac Version

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
)

  1. 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.

Python 3 Support

Is there anything serious stopping Python3 support?
I've run into some standard string handling issues, but haven't really looked into the problem...

Conversion of a struct

After converting a struct a numpy.recarray is returned. The shape of this array (in python) is empty if the the struct size is 1x1 (in matlab). Should this struct be converted to a record to avoid the confusions from the arrays?. Otherwise, I believe this array should have dimension (1,)

Thanks!

How to run .py file in different directory

I find that the python file must be placed in the same directory as .m file ,or the program can't find the .m file, so is there any solution to run python file in different directory?

I have to rename the m file or the run result would be the result before

Thanks for your great job! But I face some issue when I run your code.
I don't know if it is a bug , every time I change the content in the m file ,I have to rename the m file.
if not ,the result I run in python would be the same as what I get before I change the content in the m file.

for example:

if name == 'main':

matlab = matlab_wrapper.MatlabSession()

matlab.put('x', 2.)
matlab.eval('my_script')
y = matlab.get('y')

print("And the winner is:", y)

my_script.m
pause(3);
y = x * 2;

the result is:
And the winner is: 4.0

when I change the content in my_script.m to:
pause(3);
y = x * 5;

I still get
And the winner is: 4.0

only after I rename my_script.m to my_script.m and use matlab.eval('my_script1')
can I get:
And the winner is: 10.0

Evaluating .m file taking string as input

Hi,

I have been trying to use teh matlab_wrapper as an alternative to the Engine, which for some obscure reason, just stopped working on me recently and I just haven't been able to fix it. I'm trying to evaluate a function that takes one input: a filename. Here is what I'm doing:

import matlab_wrapper
matlab = matlab_wrapper.MatlabSession(matlab_root='/Applications/MATLAB_R2015a.app/')
matlab.put('filename','/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir')
matlab.eval('loadsir')

At which point I get the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/matlab_wrapper/matlab_session.py", line 181, in eval
    raise RuntimeError("Error from MATLAB\n{0}".format(error_string))
RuntimeError: Error from MATLAB
MATLAB:minrhs: Not enough input arguments.
Error: in fuction loadsir in file /Users/cynthiagerlein/Dropbox (Personal)/Scatterometer/Matlab/loadsir.m line 143

My Matlab function definitely only takes one argument. Does it have something to do with the fact that it's a string...?

Error: 'Matlab must be run with full administrator rights'

I am using the wrapper to use Matlab's DAQ Toolbox in Python. I have been getting this error message when I try to register my device (DT9816 ). I found an answer in this link https://www.mathworks.com/matlabcentral/answers/99879-why-does-daqregister-fail-on-windows-vista-and-windows-7-with-uac-enabled which states that I should run Matlab as administrator but I will like to know if there is a way to run the wrapper and use Matlab as administrator.
img_2469

Error when getting empty Matlab object

I am using a Matlab software package that stores data in struct arrays. These struct arrays contain a variety of other object types (other struct arrays, cell arrays, doubles, chars, etc.), which sometime turn out to be empty (i.e. they have a field name but no associated value). Using matlab_wrapper's get() method to access an empty object (whether it be contained in another struct or not) results in the following error:

matlab_wrapper/matlab_session.pyc in get(self, name)
    198         pm = self._libeng.engGetVariable(self._ep, name)
    199 
--> 200         out = mxarray_to_ndarray(self._libmx, pm)
    201 
    202 

matlab_wrapper/matlab_session.pyc in mxarray_to_ndarray(libmx, pm)
    647 
    648                 if bool(field):
--> 649                     el = mxarray_to_ndarray(libmx, field)
    650                 else:
    651                     ### uninitialized cell

matlab_wrapper/matlab_session.pyc in mxarray_to_ndarray(libmx, pm)
    674             new_arrays.append(newarr)
    675 
--> 676         if len(new_arrays) == 0:
    677             new_arrays.append(None)
    678         out = np.rec.fromarrays(new_arrays, names=field_names)

numpy/core/records.pyc in fromarrays(arrayList, dtype, shape, formats, names, titles, aligned, byteorder)
    520 
    521     if shape is None or shape == 0:
--> 522         shape = arrayList[0].shape
    523 
    524     if isinstance(shape, int):

IndexError: list index out of range

Here is a simple example to reproduce the issue:

import matlab_wrapper

matlab = matlab_wrapper.MatlabSession()
matlab.eval("empty_struct = struct('')")
empty_struct = matlab.get("empty_struct")

I've managed to resolve the issue by simply appending an empty tuple to arrays when it is empty (lines 662-663) in this gist, but you may have a better idea of how to fix this, so I'm just posting this as an issue.

For reference, I'm running Matlab 2014b 64 bits on Debian 6.0.8.

Really nice library, btw!

dimension of cellarrays

Hi,

There are issues when converting a cell array with matrices of the same size from matlab to python. The shape of the cell elements are interpreted together with the shape of the cell itself. Then there is a bug on the reshape command.

You implemented a fix for this case for the conversion of structs but not for the cell arrays.

I recommend you to implement the handling of cells in this way:

elif class_name == 'cell':
    out = np.empty(numelems,dtype='O')
    for i in range(numelems):
        cell = libmx.mxGetCell(pm, i)

        if bool(cell):
            out[i] = mxarray_to_ndarray(libmx, cell)
        else:
            ### uninitialized cell
            out[i] = None
      
    out = out.reshape(dims[:ndims], order='F')
    out = out.squeeze()

WindowsError: [Error 126] The specified module could not be found

sys info:
matlab 2010b 32 bit
python:2.7.9.0 32bit
matlab_wrapper 0.9.6

my problem:
it seems that libeng.dll in matlab can not be found.
here is my script:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import matlab_wrapper
File "", line 1
port matlab_wrapper
^
SyntaxError: invalid syntax
import matlab_wrapper
matlab=matlab_wrapper.MatlabSession()
Traceback (most recent call last):
File "", line 1, in
File "build\bdist.win32\egg\matlab_wrapper\matlab_session.py", line 112, in init
File "build\bdist.win32\egg\matlab_wrapper\matlab_session.py", line 308, in load_engine_and_libs
File "build\bdist.win32\egg\matlab_wrapper\matlab_session.py", line 791, in init
File "C:\Python27\lib\ctypes__init__.py", line 365, in init
self._handle = dlopen(self.name, mode)
WindowsError: [Error 126] The specified module could not be found
import sys
sys.path.append('C:\Program Files (x86)\MATLAB\R2010bSP2\bin\win32')
import ctypes
ctypes.cdll.LoadLibrary('C:\Program Files (x86)\MATLAB\R2010bSP2\bin\win32\libeng.dll')
Exception AttributeError: "'MatlabSession' object has no attribute 'libeng'" in <bound method MatlabSession.del of <MatlabSession:C:\Program Files (x86)\MATLAB\R2010bSP2\bin>> ignored
Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\ctypes__init
.py", line 443, in LoadLibrary
return self.dlltype(name)
File "C:\Python27\lib\ctypes__init
.py", line 365, in init
self._handle = _dlopen(self.name, mode)
WindowsError: [Error 126] The specified module could not be found
import matlab_session
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named matlab_session
sys.path.append('C:\Python27\Lib\site-packages\matlab_wrapper')
import matlab_session
matlab=matlab_wrapper.MatlabSession()
Traceback (most recent call last):
File "", line 1, in
File "build\bdist.win32\egg\matlab_wrapper\matlab_session.py", line 112, in init
engine, libeng, libmx, version = load_engine_and_libs(matlab_root, options)
File "build\bdist.win32\egg\matlab_wrapper\matlab_session.py", line 308, in load_engine_and_libs
libeng = Library('libeng')
File "build\bdist.win32\egg\matlab_wrapper\matlab_session.py", line 791, in init
self.lib = ctypes.CDLL(name, **kwargs)
File "C:\Python27\lib\ctypes__init
.py", line 365, in init
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found

path for functions outside current directory

It seems that I must start python in the same directory where the *.m files are.

For example:

import matlab_wrapper
matlab = matlab_wrapper.MatlabSession()
matlab.eval('my_script.m')

works well when my_script.m is in the current dir.

If I start python or ipython one directory up, I get:

RuntimeError: Error from MATLAB
MATLAB:undefinedVarOrFunction: Undefined variable "my_script" or function "my_script.m".

I thought about a couple of workarounds:

import os
os.chdir('examples')

or

matlab.eval('addpath("/path/to/code/examples");')

or

matlab.eval('addpath /path/to/code/examples')

but neither work on my MacOS 10.9.5, Matlab R2013b 64bits, Python 2.7.9, iPython 3.0.0

OSX bug in engGetVariable()

trying to evaluate a simple Matlab script 'test.m'

#!/usr/bin/env python
import matlab_wrapper

def main():
    matlab = matlab_wrapper.MatlabSession()
    matlab.eval('test')

if __name__ == "__main__":
    main()

returned

/usr/local/lib/python2.7/site-packages/matlab_wrapper/matlab_session.py:353: UserWarning: You are using MATLAB version 8.3 (R2014a) on OS X, which appears to have a bug in engGetVariable().  You will only be able to use arrays of type double.
  warnings.warn("You are using MATLAB version 8.3 (R2014a) on OS X, which appears to have a bug in engGetVariable().  You will only be able to use arrays of type double.")

'matlab=matlab_wrapper.MatlabSession() 'error

when I run 'matlab=matlab_wrapper.MatlabSession() ', I got this error:
' RuntimeError: It seem that you are using 64bit version of Python, but there's no matching MATLAB installation in /usr/local/MATLAB/R2015b:/usr/local/MATLAB/R2015b:/bin/glnxa64.'
Anyone can help me?thanks.

Matlab 2013a working in OSX

I just though you'd like to know that I have testing Matlab 2013a in OSX and it seems to be working okay. In fact, it looks like it works better than 2014a, since I have not observed the problem with integers.

how to close a session?

matlab = matlab_wrapper.MatlabSession(
                options='-nojvm',
                buffer_size=1000,
            )
matlab.put('x', 4.6)
matlab.eval('y=4*x')
y=matlab.get('y')

How do I then close the session?
Something like:

matlab.close()

would be useful.

I tried options='-nojvm -nodesktop -nodisplay -nosplash' which is "quieter" but the Matlab process still exist.
I could try to kill it, but surely there is a better way?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.