Calling python from Matlab: Error while calling standard modules like numpy into python script
이전 댓글 표시
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
Brandon Elford
2019년 11월 22일
I also have a similar question, anyone have some advice?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!