Can a structure variable be updated within MATLAB Function block in Simulink?
2 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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!
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Simulink Functions についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!