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

10 views (last 30 days)
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

Accepted Answer

Shrinidhi KR
Shrinidhi KR on 7 May 2020
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: https://www.mathworks.com/help/matlab/matlab-engine-for-python.html
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
insert(P,int32(0),'modulepath');
end
out = py.python_script.func(x);
end
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 trigcos.py python script:
import numpy
def find_cos(x):
return numpy.cos(x)
def find_sine(x):
return numpy.sin(x)

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!