Access Data in Data Store Memory Blocks by Using MATLAB Function Blocks
You can access data in Data Store Memory blocks from MATLAB Function blocks by defining global variables in the MATLAB Function block code. Use and access Data Store Memory block data when you want to access the data store data defined in a single model, and the model does not use a referenced model. You can access the data in multiple MATLAB Function blocks.
Define Variables in MATLAB Function Blocks
To use the Data Store Memory block data in a MATLAB Function block, or in code that the MATLAB Function block calls:
Declare a global variable in your MATLAB Function block or in the code that the MATLAB Function block calls. For example, to define the variable
myVar
as global variable, enter this code under the function declaration statement.global myVar
In the MATLAB Function block, add a variable in the Symbols pane with the same name as the global variable (since R2022a). For more information on how to define variables in MATLAB Function blocks by using the Symbols pane, see Use the Symbols pane.
Set the Scope property of the variable to
Data Store Memory
.In the model, create or open a Data Store Memory block. Assign the Data store name parameter to the same name as the global variable.
In the Data Store Memory block, set the Initial value, Data type, and Signal type parameters. The data type cannot be inherited, and the signal type must be real or complex.
Retrieve Data from Data Store Memory Blocks
This example shows how a MATLAB Function block can retrieve data stored in a Data Store Memory block.
View the Data Store Memory Block Parameters
Open the Data Store Memory block to view the parameters. On the Main tab, note that the Data store name parameter is A
. Open the Signal Attributes tab. In this example, the Initial value parameter is 25
, the Data type parameter is double
, and the Signal type parameter is real
.
To use this data in the MATLAB Function block, you cannot set Data type to Inherit: auto
or Signal type to auto
.
Inspect the MATLAB Function Block
Open the MATLAB Function block. The function code declares a global variable A
, which matches the name of the Data Store Memory block Data store name parameter. The block adds 1
to A
during each execution by using this code:
function y = fcn global A; A = A+1; y = A;
Ensure that the variable A
uses data store memory from the block:
In the Function tab, in the Prepare section, click Edit Data.
In the Symbols pane, select the variable
A
. The properties display in the Property Inspector.Ensure the Scope property is
Data Store Memory
.
Simulate the Model
Run the model. The block execution occurs at each major time step. The final output of the MATLAB Function block is 76
.
Detect Memory Access Issues
You can detect problems with data stores by configuring your model to provide run-time and compile-time diagnostics. The Configuration Parameters window and the Parameters window display diagnostics for the Data Store Memory block. For more information on using data store diagnostics, see Data Store Diagnostics.
Note
If you pass data store memory arrays to functions, optimizations such as A =
foo(A)
, this might result in the diagnostics indicating that the entire
contents of the array are read or written even though only some elements were
accessed.