problems with code call from python
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Robert Snoeberger
2016-11-28
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])
更多回答(1 个)
Bo Li
2016-11-28
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call MATLAB from Python 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!