(call python)Can matlab use multiple variables to receive separate tuple values returned from python functions?
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 30 Août 2023
Modifié(e) : Stephen23
le 30 Août 2023
"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.
Réponses (1)
Angelo Yeo
le 30 Août 2023
Modifié(e) : Angelo Yeo
le 30 Août 2023
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}
Voir également
Catégories
En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!