problems with code call from python

조회 수: 11 (최근 30일)
Pietro Castoldi
Pietro Castoldi 2016년 11월 27일
댓글: Pietro Castoldi 2016년 11월 29일
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?

채택된 답변

Robert Snoeberger
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
Pietro Castoldi
Pietro Castoldi 2016년 11월 29일
yes, thanks, it works :) i've read around it's because matlab works for the most with floats, so it's better to use floats when it is possible, and to convert the arrays in matlab.double

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bo Li
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 CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by