MATLAB Answers

problems with code call from python

15 ビュー (過去 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.
  1 件のコメント
Pietro Castoldi
Pietro Castoldi 2016 年 11 月 29 日
yes, thanks you too :)

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by