bastibe / transplant

Transplant is an easy way of calling Matlab from Python
https://transplant.readthedocs.io
Other
110 stars 26 forks source link

matlab eval function crashes transplant #78

Closed kupiqu closed 5 years ago

kupiqu commented 5 years ago

matlab.eval("x=1:100") fails:

Traceback (most recent call last):
  File "pyTransplantMatlabTest.py", line 8, in <module>
    matlab.eval("x=1:100")
  File "/home/miniconda/envs/cenv1/lib/python3.7/site-packages/transplant/transplant_master.py", line 454, in __call__
    return self._parent._call(self._fun, args, nargout=nargout)
  File "/home/miniconda/envs/cenv1/lib/python3.7/site-packages/transplant/transplant_master.py", line 618, in _call
    nargout=nargout)
  File "/home/miniconda/envs/cenv1/lib/python3.7/site-packages/transplant/transplant_master.py", line 174, in send_message
    response['stack'], response['identifier'], response['message'])
transplant.transplant_master.TransplantError: Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='. (MATLAB:m_invalid_lhs_of_assignment)
Traceback (most recent call last):
  File "/home/miniconda/envs/cenv1/lib/python3.7/site-packages/transplant/transplant_remote.m", line 129, in transplant_remote
    [results{:}] = fun(args{:});

Also, is there any other way (than with eval) to pass a vector such as 1:100 to matlab in matlab notation?

kupiqu commented 5 years ago

the message from matlab is quite weird, I guess something is not parsed properly:

sending msg:
{"identifier":"MATLAB:m_invalid_lhs_of_assignment","message":"Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='.","stack":{"file":"\/home\/miniconda\/envs\/cenv1\/lib\/python3.7\/site-packages\/transplant\/transplant_remote.m","name":"transplant_remote","line":129},"type":"error"}
msg sent.
bastibe commented 5 years ago

eval in Matlab has strange semantics, in that it errors if you try to assign its result. What you are seeing is the same thing you would see if you typed y = eval("x = 1:100") in Matlab. A more correct approach would use matlab.eval("x=1:100", nargout=0), but this fails as well, because this eval tries to assign a variable inside Transplant, which it is not allowed to.

In general, if you want to eval some code, use evalin("base", "...") instead, which works, and does what you want to do.

Also, is there any other way (than with eval) to pass a vector such as 1:100 to matlab in matlab notation?

In general, I would advise you to read the documentation of Transplant. It says right there that you need to use Numpy in Python to create the equivalent of Matlab double arrays. So, use the appropriate Numpy function, i.e. numpy.arange(1, 101).

kupiqu commented 5 years ago

I knew about that (numpy.arange), I was asking about matlab notation specifically. Thanks for the info about evalin...

Somehow I thought that matlab. instructions were parsed directly to be interpreted only in Matlab, which would be powerful, but it doesn't seem to be the case, perhaps because it's not easy to "hide" those from the python interpreter.

Thanks.