return a matrix from a MATLAB mfile(function) to python uing MATLAB API

1 visualización (últimos 30 días)
I want to send input data from python(Spyder) to MATLAB and use a function in MATLAB mfile named "triarea" and return thee variables to python. I the mfile is pretty simple:
function [e,f,g] = triarea(c,d) e = 0.5*(c.* d); f = [1 2 3]; g=zeros(4,5); end
The code I use in Spyder is: import matlab.engine eng=matlab.engine.start_matlab() aa,bb,cc=eng.triarea(1.5,2.5,nargout=3)
We have three outputs and two of them are matrixes. but when I run, only the first variable aa, which is a float is shown in spyder variable window. I wonder where the two matrixes are and how can I import matrixes from Matlab to python?

Respuesta aceptada

Stephan
Stephan el 27 de Abr. de 2018
Editada: Stephan el 27 de Abr. de 2018
Hi,
everything you do is working fine. I used your matlab code:
function [e,f,g] = triarea(c,d)
e = 0.5*(c.* d);
f = [1 2 3];
g=zeros(4,5);
end
and added 1 line with the print command to your python code to see whats going on:
import matlab.engine
eng=matlab.engine.start_matlab()
aa,bb,cc=eng.triarea(1.5,2.5,nargout=3)
print(aa, bb, cc)
Everything works - see here:
variables bb and cc are given to python as a list. The only thing you have to do is grab them out and use or convert it like you need. I used LiClipse 4.2.0, Python 3.5 with Matlab 2018a on a Windows 10 platform.
Best regards
Stephan
  1 comentario
Mani Ashouri
Mani Ashouri el 27 de Abr. de 2018
Yes, you are right, thanks. I use this code and python changes them to float64 type. m=np.array(cc) n=np.asarray(bb) I don't know the difference between float, float64 or array in Matlab. So what is the difference in use? actually, I want to import the data received from MATLAB, to another simulation program named PSCAD(for power system)

Iniciar sesión para comentar.

Más respuestas (1)

Stephan
Stephan el 27 de Abr. de 2018
Editada: Stephan el 27 de Abr. de 2018
Hi again,
see here - there you learn about the datatypes:
from matlab to python:
from python to matlab:
the float type of python is equal to matlab double type. So it should be not a problem to use this data for further operations.
best regards
Stephan

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by