Main Content

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:

  1. 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
  2. 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.

  3. Set the Scope property of the variable to Data Store Memory.

  4. In the model workspace or base workspace, create a Simulink.Signal object. Assign the Simulink.Signal object to a variable with the same name as the global variable.

  5. Set the DataType, InitialValue, and Dimensions properties of the Simulink.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:

  1. Open the Model Explorer. In the Modeling tab, in the Design section, click Model Explorer.

  2. In the left pane, expand MLFB_slsignal_model and click Model Workspace. The middle pane displays the data in the model workspace.

  3. Click the Simulink.Signal object A. In the right pane, the Model Explorer displays the properties of A. In this example, Data type is double, Dimensions is 1, Initial value is 25, and Complexity is real.

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:

  1. In the Function tab, in the Prepare section, click Edit Data.

  2. In the Symbols pane, select the variable A. The properties display in the Property Inspector.

  3. 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.

See Also

Blocks

Objects

Tools

Related Topics