problems with code call from python
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pietro Castoldi
el 27 de Nov. de 2016
Comentada: Pietro Castoldi
el 29 de Nov. de 2016
Hi, I'm trying to test a little the matlab engine inside python, i'll have to pass between python and matlab arrays and matrices, but i'm having some issues.
this is my current working code,
import matlab.engine
eng = matlab.engine.start_matlab()
b = int(input("dimmi la base: "))
h = int(input("dimmi l'altezza: "))
while b < 0:
print("\ni dati inseriti non sono validi\n")
b = int(input("dimmi la base: "))
while h < 0:
print("\ni dati inseriti non sono validi\n")
h = int(input("dimmi l'altezza: "))
res = eng.triarea(b, h)
print("l'area del triangolo è", res[0])
but as i try to give as answer an array i get this error
a =
12 int64 row vector
10 1
followed by
TypeError: array of MATLAB int64 type cannot be returned on this platform
so, somehow it understands that my first output it's an array 1x2, but can't manage it.
this it the matlab function i'm testing
function [a, x] = triarea(b,h)
a(1) = 0.5*(b.* h);
a(2)=1
i've saw these links but i've not understood how to make them work
what if the answer of the function it's then an array, a number or 2 arrays of different size?
0 comentarios
Respuesta aceptada
Robert Snoeberger
el 28 de Nov. de 2016
The error message says, "array of MATLAB int64 type cannot be returned..." One way to fix this is to return a double array from triarea instead of an int64 array. To do this, make the following change to triarea:
function [a, x] = triarea(b,h)
a(1) = 0.5*(b.* h);
a(2)=1;
a = double(a); % Convert array a to double
Another way to fix this is to call triarea with floats instead of ints. Make the following changes to your Python script:
import matlab.engine
eng = matlab.engine.start_matlab()
b = float(input("dimmi la base: ")) # convert input to float instead of int
h = float(input("dimmi l'altezza: ")) # convert input to float instead of int
while b < 0:
print("\ni dati inseriti non sono validi\n")
b = float(input("dimmi la base: ")) # convert input to float instead of int
while h < 0:
print("\ni dati inseriti non sono validi\n")
h = float(input("dimmi l'altezza: ")) # convert input to float instead of int
res = eng.triarea(b, h)
print("l'area del triangolo è", res[0])
Más respuestas (1)
Bo Li
el 28 de Nov. de 2016
Would float work for you?
import matlab.engine
eng = matlab.engine.start_matlab()
b = float(input("dimmi la base: "))
h = float(input("dimmi l'altezza: "))
A number is converted to a scalar in Python. Two arrays of different size can be represented as a cell array in MATLAB, which will be converted into a list in Python.
Ver también
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!