Initializing data store memory for Simulink global variables
11 views (last 30 days)
Show older comments
For all functions called by Simulink function block, Simulink requires the declaration of all global variables. Also these variables need to be initialized in Data store memory. Now if any of such global variables is an array, whose initial value is an empty matrix [ ] (as its size changes during the program execution), then the Simulink is not accepting the bare initial value of [ ] for any variable's data store memory. How to handle this situation, as there are too many such variables.
0 Comments
Accepted Answer
Shubham
on 11 Sep 2024
Edited: Shubham
on 11 Sep 2024
Hey Sania,
I understand that you wish to use dynamic arrays with Data Store Memory block.
Currently the Data Store Memory Blocks do not support variable-sized data and hence the block would not be able to support dynamic/growing arrays. You can refer to the following documentation for using global data from Data Store Memory Block inside a MATLAB function block: https://www.mathworks.com/help/simulink/ug/using-global-data-with-the-matlab-function-block.html#:~:text=value%20classes%20or-,variable%2Dsize%20data.,-Use%20Global%20Data
As a workaround, you can preallocate memory for the arrays that you are using and modify your MATLAB function accordingly. For instance, refer to the following Simulink model:
I have initialized the Data Store Memory with an array of size 1x10 by defining it in MATLAB workspace as follows:
arr = zeros(1,10);
arr(1) = 2;
I have written a simple MATLAB function which takes in the array and with each time step/iteration, it modifies the array. In this scenario, I am using the defined array as a stack, where the first element stores the position for the top of the stack and I make the modifications accordingly. You can refer to the following code snippet for implementing the MATLAB Function block:
function y = fcn(u)
% store a counter/index for how many values you have already stored -
% this would act as top of the stack
idx = u(1);
% modify array as needed
u(idx) = 5;
% update the counter/index and store it back in array
idx = idx+1;
u(1) = idx;
y = u;
In this way you can emulate your pre-allocated arrays to work as dynamic ones as per our need. Here is a screenshot of one of the time step of the simulation, showing how the array would grow:
Once the Simulation is complete, you can extract the desired array by simply removing the first element. If you wish, then for each of these arrays, you can maintain a separate variable to keep track of the number of elements currently present in the array.
If you have multiple such variables then the best way would be to create helper functions for handling the growing arrays.
I hope this was helpful!
4 Comments
Shubham
on 12 Sep 2024
Right-click a selection of one or more signals. Then, in the context menu, select Show Port Value Label On Selected Signal: https://www.mathworks.com/help/simulink/ug/displaying-block-outputs.html
More Answers (0)
See Also
Categories
Find more on Sources in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!