MATLAB: Problem of Call User Script from Python

matlab engine for pythonpython

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.

Best Answer

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)