Filter löschen
Filter löschen

return more than one variable from matlab to python, using python-matlab-bridge

1 Ansicht (letzte 30 Tage)
em
em am 11 Feb. 2015
Beantwortet: Bo Li am 12 Feb. 2015
It is easy to return one variable from Matlab using
res = mlab.run_func('path/to/jk.m', {'arg1': 3, 'arg2': 5})
How can I return more than one variable from Matlab. For example, my .m function is as below
function [lol, sos] = jk(args)
arg1 = args.arg1;
arg2 = args.arg2;
lol = arg1 + arg2;
sos = arg1 * arg2;
end

Antworten (1)

Bo Li
Bo Li am 12 Feb. 2015
Since R2014b, the MATLAB Engine for Python provides a package for Python to call MATLAB as a computational engine:
Using MATLAB Engine for Python, you can set the nargout to return the number of results:
For your case, it should work using following code:
>>>import matlab.engine
>>>eng = matlab.engine.start_matlab() //launch MATLAB
>>>eng.cd('theFolderofJK', nargout=0) //change to the location to where jk.m resides.
>>>res = eng.jk({'arg1':3, 'arg2': 5}, nargout = 2) //a tuple is returned for more than 1 result
(8L, 15L)

Kategorien

Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by