Calling Python function with multiple outputs in Simulink

1 回表示 (過去 30 日間)
Dino
Dino 2021 年 7 月 5 日
コメント済み: Dino 2021 年 7 月 12 日
Hello everyone,
I'm trying to call a function from a python script with matlab function block in simulink.
The problem is that the python function returns two variables and I can't read these variables in simulink.
Python function :
def test(x, y):
dr_x = x + x*y
dr_y = y + x/y
return dr_x, dr_y
And I call this function with a matlab function block in simulink:
function [dr_x, dr_y] = testQuest(x, y) % Matlab function block code
coder.extrinsic("pyversion")
coder.extrinsic("py.list")
coder.extrinsic("py.tuple")
pyversion;
dr = py.tuple({0,0}); % intialisation
dr_x = 0; % intialisation
dr_y = 0; % intialisation
coder.extrinsic('py.testQ.test')
dr = py.testQ.test(x,y);
dr_c = cell(dr);
dr_x = dr_c(1);
dr_y = dr_c(2);
When I run it, I receive an error saying: MATLAB expression 'py.testQ.test' is not numeric.
I tried different ways such as:
[dr_x, dr_y] = py.testQ.test(x,y);
instead of:
dr = py.testQ.test(x,y);
But all gives me different errors about the type of the output.
I wonder if it's possible to get multiple outputs from the python function in simulink.
Thank you !

回答 (1 件)

Yongjian Feng
Yongjian Feng 2021 年 7 月 9 日
編集済み: Yongjian Feng 2021 年 7 月 9 日
Which version of matlab and python are you using?
I tested 2021a, and calling py.testQ.test(x, y) returns a Python tuple.
ret = py.testQ.test(x, y);
ret{1}
ret{2}
  1 件のコメント
Dino
Dino 2021 年 7 月 12 日
Hello, thank you for the answer, I'm using Matlab 2018b.
Yes, on Matlab there's no problem returning tuple, however if I call the same function on simulink with matlab function block, it doesn't work. I cannot take two variables as output from one python function. So now I'm using interpreted matlab function block to call the function with .m file. It works in this way.

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

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by