Share Data Globally

When Do You Need to Use Global Data?

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.

Using Global Data with the MATLAB Function Block

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 About Data Stores and Choosing How to Store Global Data.

How MATLAB Globals Relate to Data Store Memory

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.

How to Use Globals with the MATLAB Function Block

To use global data in your MATLAB Function block, or in any code that this block calls, you must:

  1. Declare a global variable in your MATLAB Function block, or in any code that is called by the MATLAB Function block.

  2. 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 How to Use Data Store Memory Blocks and How to Use Simulink.Signal Objects.

Choosing How to Store Global Data

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.

How to Use 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:
  • You do not have to add numerous Data Store Memory blocks to your model.

  • You can load the Simulink.Signal objects in from a MAT-file.

How to Use Simulink.Signal Objects
Share data between multiple models (including referenced models).Simulink.Signal objects defined in the base workspace

    Note:   If you use Data Store Memory blocks as well as Simulink.Signal, note that using Data Store Memory blocks scopes the data to the model.

How to Use Simulink.Signal Objects

How to Use Data Store Memory Blocks

  1. Add a MATLAB Function block to your model.

  2. Double-click the MATLAB Function block to open its editor.

  3. Declare a global variable in the MATLAB Function block code, or in any MATLAB file that the MATLAB Function block code calls. For example:

    global A;
  4. Add a Data Store Memory block to your model and set the following:

    1. Set the Data store name to match the name of the global variable in your MATLAB Function block code.

    2. Set Data type to an explicit data type.

      The data type cannot be auto.

    3. Set the Signal type.

    4. Specify an Initial value.

      The initial value of the Data Store Memory block cannot be unspecified.

  5. Register the variable to the MATLAB Function block.

    1. In the Ports and Data Manager, add data with the same name as the global variable.

    2. Set the Scope of the data to Data Store Memory.

For more information on using the Ports and Data Manager, see Ports and Data Manager.

Example: Using Data Store Memory with the MATLAB Function Block

This simple model demonstrates how a MATLAB Function block uses the global data stored in Data Store Memory block A.

  1. Open the dsm_demo.mdldsm_demo.mdl model.

  2. Double-click the MATLAB Function block to open the MATLAB Function Block 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;
    
  3. In the MATLAB Function Block Editor, select Edit Data.

  4. In the Ports and Data Manager, select the data A in the left pane.

    The Ports and Data Manager displays the data attributes in the right pane. Note that A has a scope of Data Store Memory.

  5. In the model, double-click the Data Store Memory block A.

    The Block Parameters dialog box opens. Note that A has an initial value of 25.

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

How to Use Simulink.Signal Objects

  1. Create a Simulink.Signal object in the model workspace.

      Tip   Create a Simulink.Signal object in the base workspace to use the global data with multiple models.

    1. In the Model Explorer, navigate to model_name > Model Workspace in the Model Hierarchy pane.

    2. Select Add > Simulink Signal.

    3. Ensure that these settings apply to the Simulink.Signal object:

      1. Set Data type to an explicit data type.

        The data type cannot be auto.

      2. Set Dimensions to be fully specified.

        The signal dimensions cannot be -1 or inherited.

      3. Set the Complexity.

      4. Set Sample mode to Sample based.

      5. Specify an Initial value.

        The initial value of the signal cannot be unspecified.

  2. Register the Simulink.Signal object to the MATLAB Function block.

    1. In the Ports and Data Manager, add data with the same name as the Simulink.Signal object you created in the model (or base) workspace.

    2. Set the Scope of the data to Data Store Memory.

  3. Declare a global variable with the same name in the code for your MATLAB Function block.

    global Sig;

For more information on using the Ports and Data Manager, see Ports and Data Manager.

Example: Using a Simulink.Signal Object with a MATLAB Function Block

  1. Open the simulink_signal_local model. At the MATLAB command line, enter:

    addpath(fullfile(docroot, 'toolbox', 'simulink', 'examples'))
    simulink_signal_local
  2. 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;
    
  3. From the MATLAB Function Block Editor menu, select Edit Data.

  4. In the Ports and Data Manager, select the data A in the left pane.

    The Ports and Data Manager displays the data attributes in the right pane. Note that A has a scope of Data Store Memory.

  5. From the model menu, select View > Model Explorer.

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

  7. Click the Simulink.Signal object A.

    The right pane displays attributes for A, including.

    AttributeValue
    Data typedouble
    Complexityreal
    Dimensions1
    Sample modeSample based
    Initial value 5

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

Using Data Store Diagnostics to Detect Memory Access Issues

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.

Limitations of Using Shared Data in MATLAB Function Blocks

There is no Data Store Memory support for:

  • MATLAB structures

  • Variable-sized data

Was this topic helpful?