Access Data in Simulink.Signal
Objects by Using MATLAB
Function Blocks
You can access data in Simulink.Signal
objects from MATLAB
Function blocks by defining global variables in the MATLAB Function
block code. Use and access Simulink.Signal
object data when you define a data
store that you want to access in multiple models that contain MATLAB Function
blocks, or if you need to access many data stores in your MATLAB Function
blocks. You can access the data in multiple MATLAB Function blocks.
Define Variables in MATLAB Function Blocks
To define global data with a Simulink.Signal
object and use the data in
a MATLAB Function block or in the code that the 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 workspace or base workspace, create a
Simulink.Signal
object. Assign theSimulink.Signal
object to a variable with the same name as the global variable.Set the
DataType
,InitialValue
, andDimensions
properties of theSimulink.Signal
object. The data type cannot be inherited, and the signal type must be real or complex.
You can scope Simulink.Signal
objects to the model or base workspace.
You can define the Simulink.Signal
objects in the Model Explorer or load
them from a MAT file.
Retrieve Data From Simulink.Signal
Objects
This example shows how a MATLAB Function block can use the data stored in a Simulink.Signal
object.
View the Simulink.Signal
Object Properties
View the properties of the Simulink.Signal
object:
Open the Model Explorer. In the Modeling tab, in the Design section, click Model Explorer.
In the left pane, expand
MLFB_slsignal_model
and click Model Workspace. The middle pane displays the data in the model workspace.Click the
Simulink.Signal
objectA
. In the right pane, the Model Explorer displays the properties ofA
. In this example, Data type isdouble
, Dimensions is1
, Initial value is25
, and Complexity isreal
.
To use this data in the MATLAB Function block, you cannot set Data type or Complexity 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 Simulink.Signal
object. 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 the data from the object:
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
.