Why am I getting 'MatlabExecutionError: Too many output arguments.' when using the Matlab engine in Python?

조회 수: 69 (최근 30일)
Here is the code I am running on each side:
Python
>>> m1 = matlab.engine.start_matlab()
>>> testpath = 'Path/to/file/containing-testfunc.m'
>>> m1.addpath(testpath)
*lots of path gibberish*
>>> m1.testfunc()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Library/Python/2.7/site-packages/matlab/engine/matlabengine.py"
, line 79, in __call__
_stderr).result()
File "/Library/Python/2.7/site-packages/matlab/engine/futureresult.py"
, line 107, in result
self._future,self._nargout, None, out=self._out, err=self._err)
MatlabExecutionError: Too many output arguments.
I have double checked the path to testfunc.m, and I suspect that Matlab is able to find it because otherwise it would say it could not find it.
Here are the contents of testfunc.m:
function [] = testfunc()
a = 2
end
As indicated by the name, this function is only a test because I found this error using a more complicated function. I created the testfunc() just to try to narrow down the reasons for which I might be getting this error. I am able to run testfunc() (and my more complicated function) from the Matlab gui if I use
Python
>>> m1.desktop(nargout=0)
Matlab
>> testfunc()
a =
2
This leads me to believe there is an issue with the Matlab engine or the way I am using it.
It might be important to point out that built in Matlab functions seem to be working fine in the engine for example:
m1.workspace['testvar'] = 10
I am only having trouble using functions that I include myself.
Any help would be greatly appreciated.
  댓글 수: 4
rui gao
rui gao 2019년 11월 13일
Thanks, it works.
We can also put some input parameters before the 'nargout=0' for the situation that the function need some input.
For example:
eng = matlab.engine.start_matlab()
eng.MatlabFunctionName(1, 2, 3, nargout=0)
eng.quit()
Nagabhushan SN
Nagabhushan SN 2020년 4월 20일
Thanks! very useful. Is it possible to upvote a comment here? Like in SO

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

채택된 답변

Bo Li
Bo Li 2015년 7월 1일
Try this:
>>>m1.testfunc(nargout=0)
By default, the nargout is set to 1 in Python Engine:
Since testfunc.m does not return any result, you need to specify nargout to be 0 like what you did to launch the MATLAB Desktop:
>>>m1.desktop(nargout=0)

추가 답변 (1개)

Khaoula Mosbahi
Khaoula Mosbahi 2021년 8월 9일
hello everyone , i need your help , when i run matlab code en matlab i found this result
but when i try this code:
eng = matlab.engine.start_matlab()
kh=eng.Main(7, nargout=0)
print(kh)
the result always NONE
help please

카테고리

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