Problem of Call User Script from Python

3 views (last 30 days)
Thanks everyone!
I installed matlab engine for python successfully and I was able to call matlab functions from python. This is the link I followed: http://www.mathworks.com/help/matlab/matlab-engine-for-python.html
I tried to call matlab script in python. However I cannot run the script. I followed the instruction in this link: http://www.mathworks.com/help/matlab/matlab_external/call-user-script-and-function-from-python.html
And I get this error information:
%run "C:\Users\czhan139\Documents\PhD research\Experiments & Data Processing\python_learning\test.py" --------------------------------------------------------------------------- MatlabExecutionError Traceback (most recent call last) C:\Users\czhan139\Documents\PhD research\Experiments & Data Processing\python_learning\test.py in module() 1 import matlab.engine 2 eng = matlab.engine.start_matlab() ----> 3 eng.triarea(nargout=0)
C:\Users\czhan139\AppData\Local\Enthought\Canopy\User\lib\site-packages\matlab\engine\matlabengine.pyc in _call_(self, *args, **kwargs) 78 else: 79 return FutureResult(self._engine(), future, nargs, _stdout, ---> 80 _stderr).result() 81 82
C:\Users\czhan139\AppData\Local\Enthought\Canopy\User\lib\site-packages\matlab\engine\futureresult.pyc in result(self, timeout) 107 108 self._result = pythonengine.getFEvalResult( --> 109 self._future,self._nargout, None, out=self._out, err=self._err) 110 self._retrieved = True 111 return self._result
MatlabExecutionError: Undefined function or variable 'triarea'.
Undefined function or variable 'triarea'.
It seems the path of the file is wrong. Then I use cd command to set the path of the .m file and the .py file (they are in the same folder) in matlab. Then I tried again. Still not working!
Sorry for the poor English. Is there anyone who has run the matlab script successfully?
Thank you.

Accepted Answer

Bo Li
Bo Li on 13 Apr 2015
The error message indicates that MATLAB cannot find a variable called 'triarea' or a M function named with 'triarea.m'.
Assume your 'triarea.m' is located in folder 'c:\foo', what is the output of following commands?
>>>eng.cd(r'c:\foo', nargout=0)
>>>eng.ls(nargout=0)
  3 Comments
Paul Wódz
Paul Wódz on 18 Jul 2016
Edited: Paul Wódz on 18 Jul 2016
I'been trying for hours run a method on an engine instance, which was initialized in python script in a different directory than the matlab script. The cd command fixed this issue for me. Thanks!
Arthur dos Santos
Arthur dos Santos on 14 Dec 2021
Dude, I love you. It works, thank you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!