You might need to use global data with a MATLAB Function block if:
You have multiple MATLAB® functions that use global variables and you want to call these functions from MATLAB Function blocks.
You have an existing model that uses a large amount of global data and you are adding a MATLAB Function block to this model, and you want to avoid cluttering your model with additional inputs and outputs.
You want to scope the visibility of data to parts of the model.
In Simulink®, you store global data using data store memory.
You implement data store memory using either Data
Store Memory blocks or Simulink.Signal
objects. How you store global data
depends on the number and scope of your global variables. For more
information, see Local and Global Data Stores and Choosing How to Store Global Data.
In MATLAB functions in Simulink, global declarations are not mapped to the MATLAB global workspace. Instead, you register global data with the MATLAB Function block to map the data to data store memory. This difference allows global data in MATLAB functions to inter-operate with the Simulink solver and to provide diagnostics if they are misused.
A global variable resolves hierarchically to the closest data store memory with the same name in the model. The same global variable occurring in two different MATLAB Function blocks might resolve to different data store memory depending on the hierarchy of your model. You can use this ability to scope the visibility of data to a subsystem.
To use global data in your MATLAB Function block, or in any code that this block calls, you must:
Declare a global variable in your MATLAB Function block, or in any code that is called by the MATLAB Function block.
Register a Data Store Memory block or Simulink.Signal
object
that has the same name as the global variable with the MATLAB
Function block.
For more information, see Storing Data Using Data Store Memory Blocks and Storing Data Using Simulink.Signal Objects.
The following table summarizes whether to use Data Store Memory blocks or
Simulink.Signal
objects.
If you want to: | Use: | For more information: |
---|---|---|
Use a small number of global variables in a single model that does not use model reference. | Data Store Memory blocks. Note Using Data Store Memory blocks scopes the data to the model. | Storing Data Using Data Store Memory Blocks |
Use a large number of global variables in a single model that does not use model reference. | Simulink.Signal objects defined in the model workspace.
Simulink.Signal objects offer these advantages:
| Storing Data Using Simulink.Signal Objects |
Share data between multiple models (including referenced models). | Simulink.Signal objects defined in the base workspaceNote If you use Data Store Memory blocks as well as
| Storing Data Using Simulink.Signal Objects |
This model demonstrates how a MATLAB Function block uses the global data
stored in a Data Store Memory block A
.
Open the dsm_demo.mdl model.
Double-click the MATLAB Function block to open the MATLAB Function Block Editor.
The MATLAB Function block code declares a global variable
A
. The block modifies the value of A
during each
execution.
function y = fcn %#codegen global A; A = A+1; y = A;
Make sure the global variable is registered to the MATLAB Function block. See Adding Data to a MATLAB Function Block.
In the MATLAB Function Block Editor, select Edit Data to open the Ports and Data Manager dialog box.
In the Ports and Data Manager, select the data A in the left pane. This data uses the same name as the global variable.
The Scope of the data is set to Data Store
Memory
.
See also Ports and Data Manager.
Double-click the Data Store Memory block A
. In the
Block Parameters dialog box, you see that the Data store name
A
matches the global variable name. The block has an initial value of
25
.
When you add a Data Store Memory to your model:
Set the Data store name to match the name of the global variable in your MATLAB Function block code.
Set Data type to an explicit data type. The data type
cannot be auto
.
Set the Signal type and specify an Initial value.
Simulate the model.
The MATLAB Function block reads the initial value of global data
stored in A
and updates the value of A
each time
it executes.
Simulink.Signal
ObjectsThis model demonstrates how a MATLAB Function block uses the global data
stored in a Simulink.Signal
object A
.
Open the simulink_signal_local model.
The model uses a Simulink.Signal
object in the model workspace.
Note
To use the global data with multiple models, create a
Simulink.Signal
object in the base workspace .
Make sure that the Simulink.Signal
object is added to the Model Explorer.
In the Modeling tab, click Model Explorer.
In the left pane of the Model Explorer, select the model workspace for the
simulink_signal_local
model.
The Contents pane displays the data in the model workspace.
Click the Simulink.Signal
object A
.
In the right pane, make sure that the Model Explorer displays these attributes
for A
.
Attribute | Value |
---|---|
Data type | double |
Complexity | real |
Dimensions | 1 |
Initial value | 5 |
See also Model Explorer.
Double-click the MATLAB Function block to open its editor.
The MATLAB Function block modifies the value of global data
A
each time it executes.
function y = fcn %#codegen global A; A = A+1; y = A;
Make sure the Simulink.Signal
object is registered to the
MATLAB Function block.
In the MATLAB Function Block Editor, select Edit Data to open the Ports and Data Manager dialog box.
In the Ports and Data Manager, select the data A in the left pane. This data uses the same name as the global variable.
Set the Scope of the data to Data Store
Memory
.
See also Ports and Data Manager.
Simulate the model.
The MATLAB Function block reads the initial value of global data
stored in A
and updates the value of A
each time
it executes.
You can configure your model to provide run-time and compile-time diagnostics for avoiding
problems with data stores. Diagnostics are available in the Configuration Parameters dialog
box and the parameters dialog box for the Data Store Memory block. These
diagnostics are available for Data Store Memory blocks only, not for
Simulink.Signal
objects. 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)
might result in the code generation
software marking the entire contents of the array as read or written
even though only some elements were accessed.
There is no Data Store Memory support for:
MATLAB structures
Variable-sized data