Problem of Call User Script from Python

조회 수: 3 (최근 30일)
Cheng Zhang
Cheng Zhang 2015년 4월 13일
댓글: Arthur dos Santos 2021년 12월 14일
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.

채택된 답변

Bo Li
Bo Li 2015년 4월 13일
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
Paul Wódz
Paul Wódz 2016년 7월 18일
편집: Paul Wódz 2016년 7월 18일
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 2021년 12월 14일
Dude, I love you. It works, thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by