Storing variables in Simulink

4 ビュー (過去 30 日間)
Joseph Criniti
Joseph Criniti 2023 年 5 月 15 日
回答済み: Naga 2024 年 10 月 23 日
I'm using a simulink function block that (for now) has no inputs and one output. It needs to be able to call 3 initial states stored in arrays and append those arrays with the new values every time step. I've tried to make the variables global and initilize them in the matlab workspace but this doesnt seem to work. I have seen solutions using data store memory blocks and Simulink.Signal objects but I do not understand how to use these methods in practice.
Thank you.
  1 件のコメント
FannoFlow
FannoFlow 2023 年 5 月 16 日
Are these arrays growing dynamically? or do they have a fixed size and you are just assigning new values to them?

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

回答 (1 件)

Naga
Naga 2024 年 10 月 23 日
Hi,
To maintain state information across Simulink time steps without using global variables, you can use either Simulink.Signal objects or Simulink Data Store Memory blocks. Here's how:
Here's how you can implement each option:
Using Simulink.Signal Objects:
  1. Define a structure array in MATLAB for your initial states:
myStates = struct('initState1', zeros(5,1), 'initState2', zeros(2,1), 'initState3', zeros(3,1));
2. Add Simulink.Signal blocks for each state in your model and connect them to your function block.
3. Access and update these states inside the function block using get_param:
mySignal1 = get_param('myModelName/Signal1', 'RuntimeObject');
Using Simulink Data Store Memory Blocks:
  1. Use Simulink Data Store Memory blocks to define and initialize your state arrays.
  2. Connect these blocks to your function block.
  3. Access and update the data store values within the function block at each time step.
Both methods allow you to manage and update state arrays across time steps effectively.

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by