(call python)Can matlab use multiple variables to receive separate tuple values returned from python functions?

13 vues (au cours des 30 derniers jours)
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
ans =
PythonEnvironment with properties: Version: "3.10" Executable: "/usr/bin/python3" Library: "libpython3.10.so.1.0" Home: "/usr" Status: NotLoaded ExecutionMode: InProcess
python module function( myModule1.py and myModule2.py in current work directory):
!unzip myModules.zip
Archive: myModules.zip inflating: myModule1.py inflating: myModule2.py
--------------------------------------------
# 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)
outs =
Python tuple with values: (3.0, -1.0) Use string, double or cell function to convert to a MATLAB array.
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)
No method 'py.myModule1.fun' with matching signature found.
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
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.
cui,xingxing
cui,xingxing le 30 Août 2023
@Stephen23 Here is the return from Python is tuple type, I know that by default MATLAB will convert the tuple type to an array of cell type, I believe that only for the return of the tuple unpacking operation assignment can be achieved, tuple inside the element can be guaranteed not to need to be converted, keep the default Python native object can be.In this respect, this does not break existing MATLAB syntax rules!

Connectez-vous pour commenter.

Réponses (1)

Angelo Yeo
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
Archive: myModules.zip inflating: myModule1.py inflating: myModule2.py
x = 1;
y = 2;
out1 = py.myModule1.fun(x, y);
out1{1}, out1{2}
ans = 3
ans = -1
  1 commentaire
cui,xingxing
cui,xingxing 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."
@Angelo Yeo I know this, maybe I didn't articulate the problem with my description.
What I'm asking is that it would be nice if the returned tuple could be received separately by multiple MATLAB variables, so that the indexing operation could be omitted (although it's a bit of a hassle).
Note:In python, the returned tuple could be received separately by multiple Python variables.
----------------------------------------------------------------
def person(): #Python Code
return "bob", 32, "boston"
out1,out2,out3= person() # could be received separately by multiple Python variables.
print(out1,out2,out3)
----------------------------------------------------------------

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by