problems with code call from python
显示 更早的评论
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?
采纳的回答
更多回答(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.
类别
在 帮助中心 和 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!