Initializing data store memory for Simulink global variables
10 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Shubham
2024 年 9 月 11 日
編集済み: Shubham
2024 年 9 月 11 日
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 件のコメント
Shubham
2024 年 9 月 12 日
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Sources についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!