Problem of Call User Script from Python

3 vues (au cours des 30 derniers jours)
Cheng Zhang
Cheng Zhang le 13 Avr 2015
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.

Réponse acceptée

Bo Li
Bo Li le 13 Avr 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 commentaires
Paul Wódz
Paul Wódz le 18 Juil 2016
Modifié(e) : Paul Wódz le 18 Juil 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 le 14 Déc 2021
Dude, I love you. It works, thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by