Problem of Call User Script from Python

8 ビュー (過去 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 Center および File ExchangeCall MATLAB from Python についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by