Unable to Unpack Variables from Python to MATLAB Function in Simulink Model

3 ビュー (過去 30 日間)
Arseny
Arseny 2024 年 8 月 4 日
回答済み: Gautam 2024 年 11 月 28 日
Hi everyone,
I'm beginner in Matlab and I'm working on a project where I need to replace a scalar control block for an induction motor in Simulink with my own control block written in Python. The Python function calculates three variables that I need to pass back to MATLAB Function block, and convert to vector.
However, I'm running into an issue where MATLAB seems to treat these variables as non-iterable and can't unpack them properly. Even though my Python function returns a tuple with the three variables, MATLAB can't seem to process them as intended.
Here’s a summary of the setup:
MATLAB Function:
function Vabc = Run_in_Python(fRef)
Vabc = zeros(3, 1);
coder.extrinsic('py.ControllerPython.calc');
result = cell(py.ControllerPython.calc(fRef));
Vabc = vertcat(result);
end
Error:
Python Error: TypeError: cannot unpack non-iterable int object Error in calc Error in 'InductionMachineScalar/Control/Scalar controller/MATLAB Function' (line 6)
Thanks in advance!
  3 件のコメント
Arseny
Arseny 2024 年 8 月 4 日
編集済み: Arseny 2024 年 8 月 4 日
Thank you,
but if I use:
Vabc = vertcat(result{:});
I get that error:
Error:Brace indexing is not supported for variables of this type.
Function 'Control/Scalar controller/MATLAB Function' (#25.182.191), line 8, column 20:
"result{:}"
Launch diagnostic report.
Error:Errors occurred during parsing of 'InductionMachineScalar/Control/Scalar controller/MATLAB Function'.
Error:Errors occurred during parsing of 'InductionMachineScalar/Control/Scalar controller/MATLAB Function'.
Paul
Paul 2024 年 8 月 5 日
Can you run these commands
result = cell(py.ControllerPython.calc(fRef));
whos result
at the Matlab command prompt and copy the commands and the results back to here so we can see the type and size of result?

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

回答 (1 件)

Gautam
Gautam 2024 年 11 月 28 日
Hello Arseny,
You can unpack the elements of the tuple into a vector by first converting it to “double” and then using the “vertcat” function on it. We’d want to convert to double as converting tuples to double allows you to leverage MATLAB's matrix operations and built-in functions without additional type handling.
Just replace the lines 4 and 5 of your code with
result = double(py.ControllerPython.calc(fRef));
Vabc = vertcat(result(:));

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by