Calling python from Matlab: Error while calling standard modules like numpy into python script
Show older comments
Hi all,
I am trying to call a python script from a matlab function and inside the python script I need to use numpy, but when I try to do it I get different kinds of errors. For example:
--------MATLAB CODE-------------
function varargout = tree_eval(x)
P = py.sys.path;
if count(P,'modulepath') == 0
insert(P,int32(0),'modulepath');
end
out = py.python_script.func(x);
end
------------PYTHON CODE ------------
import numpy
def func(x):
a = numpy.cos(x)
return a
If I leave "import numpy" outside the function then the code get stuck somewhere and doesn't finish its execution. While if I write the import statement inside the python function I get the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/matlab/engine/matlabengine.py", line 71, in __call__
_stderr, feval=True).result()
File "/usr/local/lib/python3.6/dist-packages/matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/usr/local/lib/python3.6/dist-packages/matlab/engine/fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.EngineError: MATLAB function cannot be evaluated
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/matlab/engine/__init__.py", line 193, in __exit_engines
eng().exit()
File "/usr/local/lib/python3.6/dist-packages/matlab/engine/matlabengine.py", line 232, in exit
pythonengine.closeMATLAB(self.__dict__["_matlab"])
SystemError: MATLAB process cannot be terminated.
Can anyone help me with this issue? Thank you in advance!
Francesco
1 Comment
Brandon Elford
on 22 Nov 2019
I also have a similar question, anyone have some advice?
Accepted Answer
More Answers (0)
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!