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

37 次查看(过去 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

采纳的回答

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 CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by