Can a structure variable be updated within MATLAB Function block in Simulink?

2 ビュー (過去 30 日間)
afaq ahmad
afaq ahmad 2023 年 9 月 15 日
コメント済み: afaq ahmad 2024 年 7 月 24 日
I am running a simulation of a physical system in Simlink using a MATLAB function block. In this regard, I have some parameters that are defined in a structure in MATLAB workspace. I passed that structure by defining it as a parameter to the MATLAB function block. However, the code in the block attempts to add more fields to that structure, but the Simulink won't allow me to do that, giving the following error:
new fields cannot be added when structure has been read or used
Is there a way I can dynamically update the structure within the Function block?
Thanks

採用された回答

Suman
Suman 2024 年 7 月 24 日
Hi Afaq,
There are some limitations to using variables in MATLAB function block as opposed to the way it would run in a MATLAB environment. These restrictions are likely imposed to allow effiecient C code generation.
One of the restrictions is that you cannot add fields to the struct on the go. Please read about other restrictions while using struct in MATLAB function block here: https://www.mathworks.com/help/simulink/ug/how-working-with-structures-is-different-for-code-generation.html
You can create a struct with all the fields and assign it to zero at the beginning and then go about assigning the fields.
function a = b(t)
struct = struct('f1', 0, 'f2', 1);
struct.f1=t;
a=struct.f2;
end
If you need to add fields to the struct, please refer to using Variable-sized block variables: https://www.mathworks.com/help/simulink/ug/declare-variable-size-inputs-and-outputs.html
I hope that helps!
  1 件のコメント
afaq ahmad
afaq ahmad 2024 年 7 月 24 日
Hi Suman,
Thanks for your response. Actually, I had found a way around by using the S-function block instead of Matlab function block. S-function block is slow, but it gets the job done.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by