How to handle multi Matlab return values in Python

54 visualizaciones (últimos 30 días)
Jason Shih
Jason Shih el 1 de Jul. de 2015
Comentada: Bo Li el 6 de Feb. de 2017
I'm using Matlab 2014b, and I want to call Matlab function from Python(2.7.6). Here is my Python script:
import matlab.engine as meng
import matlab as mat
eng = meng.start_matlab()
try:
x,y = eng.Fun_Py(3,4)
print x
print y
eng.quit()
except:
print "error in python script"
eng.quit()
and here is my Matlab script:
function [x,y]=Fun_Py(a,b) x=zeros(a,b); y=sum(x); end
When I ran the Python scipt, I got "error in python script".How can I solve this problem? Any help is appreciate.
Jason Shih

Respuesta aceptada

Bo Li
Bo Li el 1 de Jul. de 2015
There are two things need to be done.
1. Add the folder where Fun_Py.m is located into MATLAB path from MATLAB command window:
Or you can change the current folder in MATLAB to where Fun_Py.m is located, for example if it is located in c:\temp:
eng.cd(r'C:\temp)
2. Set the number of output to 2 if 2 output is expected:
x,y = eng.Fun_Py(3,4, nargout=2)
  3 comentarios
Petru-Daniel Tudosiu
Petru-Daniel Tudosiu el 5 de Feb. de 2017
@Bo Li I tried what you said and it says that the function did not expect that explicit variable. I am using Python 3.4 with Matlab R2016b. Can You please help me?
Bo Li
Bo Li el 6 de Feb. de 2017
I need more details maybe sample code to understand "the function did not expect that explicit variable".
A simple MATLAB function that returns multiple output is gcd:
>>> a,b,c=eng.gcd(8,10, nargout=3)
>>> a,b,c
(2L, -1L, 1L)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Call MATLAB from Python en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by