Calling python from Matlab: Error while calling standard modules like numpy into python script

17 ビュー (過去 30 日間)
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
out = py.python_script.func(x);
------------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/", line 71, in __call__
_stderr, feval=True).result()
File "/usr/local/lib/python3.6/dist-packages/matlab/engine/", line 67, in result
return self.__future.result(timeout)
File "/usr/local/lib/python3.6/dist-packages/matlab/engine/", 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/", line 193, in __exit_engines
File "/usr/local/lib/python3.6/dist-packages/matlab/engine/", line 232, in exit
SystemError: MATLAB process cannot be terminated.
Can anyone help me with this issue? Thank you in advance!
  1 件のコメント
Brandon Elford
Brandon Elford 2019 年 11 月 22 日
I also have a similar question, anyone have some advice?



Shrinidhi KR
Shrinidhi KR 2020 年 5 月 7 日
You do not need MATLAB Engine API for Python when you want to call python functions or script from Matlab script. It is useful when you want to call matlab from python script.
This documentation link about calling matlab from python explains it:
Hence I am not sure of the python errors that you are getting from matlab engine, which you say arises when you write import statement inside the python function (func).
In your case, you need to call python script / function ('func' in your case) from matlab. The matlab code that you have written has a problem, varargout return variable that you have used is not assigned any value inside the function. Variable 'out' is being assigned the value of the output of python function ('func') hence you may not be getting any output.
If you modify the function definition as follows, it should work fine with your python code:
function out = tree_eval(x)
P = py.sys.path;
if count(P,'modulepath') == 0
out = py.python_script.func(x);
Speaking of the positioning of import statement in python code, it worked fine for me in both cases, either inside function or outside. The only difference is the way in which matlab loads it, is if you have the import outside function it loads as module. This can be checked in this way:
>> py.importlib.import_module('trigocos')
ans =
Python module with properties:
find_sine: [1×1 py.function]
find_cos: [1×1 py.function]
numpy: [1×1 py.module]
This is the content of python script:
import numpy
def find_cos(x):
return numpy.cos(x)
def find_sine(x):
return numpy.sin(x)

その他の回答 (0 件)


Help Center および File ExchangeCall Python from MATLAB についてさらに検索




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by