Storing variables in Simulink

5 visualizaciones (últimos 30 días)
Joseph Criniti
Joseph Criniti el 15 de Mayo de 2023
Respondida: Amith el 22 de Mayo de 2023
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 comentario
FannoFlow
FannoFlow el 16 de Mayo de 2023
Are these arrays growing dynamically? or do they have a fixed size and you are just assigning new values to them?

Iniciar sesión para comentar.

Respuestas (1)

Amith
Amith el 22 de Mayo de 2023
Hi,
As per my understanding of the question you want to store states between simulink time steps and use them like global variables. These below options can be used - Simulink.Signal object or Simulink Data Store Memory blocks.
Here's how you can implement each option:
Using Simulink.Signal Objects:
1. Define a Simulink.Signal object for each of your initial state arrays by going to MATLAB command prompt and using `struct` function to create a structure array called `myStates`. Example code for creating a structure array is as follows:
myStates = struct('initState1', zeros(5,1), 'initState2', zeros(2,1), 'initState3', zeros(3,1));
2. In your Simulink model, create a Simulink.Signal object for each initial state array by using Simulink.Signal block from Simulink Library Browser.
3. Connect each of those Simulink.Signal objects as inputs to your function block.
4. Inside function block, use `get_param` function to retrieve the values of the Simulink.Signal objects in the MATLAB workspace as shown below:
mySignal1 = get_param('myModelName/Signal1', 'RuntimeObject');
initState1 = myStates.initState1;
5. Now you can use the initial state arrays inside your function block as if they were global variables, and each time step you can assign the new values to their appropriate elements.
Using Simulink Data Store Memory Blocks:
1. Define a Simulink Data Store by using the Simulink Data Store Memory block from the Simulink Library Browser.
2. Inside the block dialogue box, define the data store name and the initial values of your arrays.
3. In your Simulink model, create a Simulink Data Store Memory block for each of the arrays you want to store.
4. Connect each of those Simulink Data Store Memory blocks as inputs to your function block.
5. Inside your function block, use the Simulink Data Store Memory blocks as you would global variables, updating their values each time step.These two methods will eliminate the need for global variables and allow you to access and update the state arrays between time steps as necessary.

Categorías

Más información sobre Programmatic Model Editing en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by