(call python)Can matlab use multiple variables to receive separate tuple values returned from python functions?
조회 수: 14 (최근 30일)
이전 댓글 표시
When a python module function is called in matlab and the python function returns a tuple value, is it possible to assign each element of the tuple to a separate variable for storage?
Note: Instead of storing a return variable and then indexing out each element.
Example code:
pyenv
python module function( myModule1.py and myModule2.py in current work directory):
!unzip myModules.zip
--------------------------------------------
# myModule1.py
def fun(a,b):
return a+b,a-b
---------------------------------------------
# myModule2.py
def fun(a,b):
return a+b,a-b
arg1 = 1
arg2 = 2
out1,out2 = fun(arg1,arg2)
---------------------------------------------
Then i do test in matlab( in current work directory):
x = 1;
y = 2;
outs = py.myModule1.fun(x,y)
Obviously, the function "fun" in the above python module myModule1 successfully returns a tuple value, which contains 2 elements, but I try to save the values in the tuple in out1 and out2 below, but I get an error directly?Will this syntax be supported in future versions?
[out1,out2] = py.myModule1.fun(x,y)
However, with myModule2 it is possible to receive multiple parameters separately, why is that?
[out1,out2] = pyrunfile("myModule2.py",["out1","out2"])
out1 =
Python int with properties:
denominator: [1×1 py.int]
imag: [1×1 py.int]
numerator: [1×1 py.int]
real: [1×1 py.int]
3
out2 =
Python int with properties:
denominator: [1×1 py.int]
imag: [1×1 py.int]
numerator: [1×1 py.int]
real: [1×1 py.int]
-1
댓글 수: 2
Stephen23
2023년 8월 30일
편집: Stephen23
2023년 8월 30일
"Can matlab use multiple variables to receive separate tuple values returned from python functions?"
No.
What is being returned is a tuple. The corresponding data type in MATLAB would be a cell array. What you are asking for is that when a cell array gets returned by a MATLAB function it may get split into lots of separate arrays, depending on some non-trivial unpacking rules defined by some third-party (and those rules also change over time, which TMW has no control over). That would be a very major change to how MATLAB works, for a relatively rare use-case. It would mean any code that relies on comma-separated lists and/or multiple outputs would be ambigous to parse, breaking almost all existing MATLAB code.
답변 (1개)
Angelo Yeo
2023년 8월 30일
편집: Angelo Yeo
2023년 8월 30일
I believe that is the nature of Python functions with multiple outputs. A Python function with comma-separated outputs returns a tuple. See the simple example below.
----------------------------------------------------------------
def person(): #Python Code
return "bob", 32, "boston"
print(person())
----------------------------------------------------------------
The result is ('bob', 32, 'boston').
One question: Why wouldn't indexing work for you? I believe the simplest is to use curly bracket to access the elements in tuples.
!unzip myModules.zip
x = 1;
y = 2;
out1 = py.myModule1.fun(x, y);
out1{1}, out1{2}
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!