Main Content

Share Data Globally

When Do You Need to Use Global Data?

You might need to use global data with aMATLAB Functionblock if:

  • You have multiple MATLAB®functions that use global variables and you want to call these functions fromMATLAB Functionblocks.

  • You have an existing model that uses a large amount of global data and you are adding aMATLAB Functionblock 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 eitherData Store Memoryblocks orSimulink.Signalobjects. How you store global data depends on the number and scope of your global variables. For more information, seeLocal and Global Data StoresandChoosing How to Store Global Data.

HowMATLABGlobals 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 theMATLAB Function块映射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 differentMATLAB Functionblocks 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 yourMATLAB Functionblock, or in any code that this block calls, you must:

  1. Declare a global variable in yourMATLAB Functionblock, or in any code that is called by theMATLAB Functionblock.

  2. Register aData Store Memoryblock orSimulink.Signalobject that has the same name as the global variable with theMATLAB Functionblock.

For more information, seeStoring Data Using Data Store Memory BlocksandStoring Data Using Simulink.Signal Objects.

Choosing How to Store Global Data

The following table summarizes whether to useData Store Memoryblocks orSimulink.Signalobjects.

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

Note

UsingData Store Memoryblocks 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.Signalobjects defined in the model workspace.Simulink.Signalobjects offer these advantages:

  • You do not have to add numerousData Store Memory块哟ur model.

  • You can load theSimulink.Signalobjects in from a MAT-file.

Storing Data Using Simulink.Signal Objects
Share data between multiple models (including referenced models).

Simulink.Signalobjects defined in the base workspace

Note

If you useData Store Memoryblocks as well asSimulink.Signal, note that usingData Store Memoryblocks scopes the data to the model.

Storing Data Using Simulink.Signal Objects

Storing Data Using Data Store Memory Blocks

This model demonstrates how aMATLAB Functionblock uses the global data stored in aData Store MemoryblockA.

  1. Open thedsm_demo.mdlmodel.

    This image shows a MATLAB Function block set to output to a Display block. The block uses global data stored in a Data Store Memory block.

  2. Double-click theMATLAB Functionblock to open theMATLAB Function Block Editor.

    TheMATLAB Functionblock code declares a global variableA. The block modifies the value ofAduring each execution.

    functiony = fcn%#codegenglobalA; A = A+1; y = A;

  3. Make sure the global variable is registered to theMATLAB Functionblock. SeeCreate and Define MATLAB Function Block Variables.

    1. In theModelingtab, in theDesignsection, clickSymbols Pane.

    2. In theSymbolspane, select the dataA. This data uses the same name as the global variable. Right click and selectInspect...to open the Property Inspector.

    3. In the Property Inspector, theScopeof the data is set toData Store Memory.

  4. Double-click theData Store MemoryblockA. In the Block Parameters dialog box, you see that theData store nameAmatches the global variable name. The block has an initial value of25.

    When you add aData Store Memoryto your model:

    1. Set theData store nameto match the name of the global variable in yourMATLAB Functionblock code.

    2. SetData typeto an explicit data type. The data type cannot beauto.

    3. Set theSignal typeand specify anInitial value.

  5. Simulate the model.

    TheMATLAB Functionblock reads the initial value of global data stored inAand updates the value ofAeach time it executes.

Storing Data UsingSimulink.SignalObjects

This model demonstrates how aMATLAB Functionblock uses the global data stored in aSimulink.SignalobjectA.

  1. Open thesimulink_signal_localmodel.

    This image shows a MATLAB Function block set to output to a Display block.

    The model uses aSimulink.Signalobject in the model workspace.

    Note

    To use the global data with multiple models, create aSimulink.Signalobject in the base workspace .

  2. Make sure that theSimulink.Signalobject is added to the Model Explorer.

    1. In theModelingtab, clickModel Explorer.

    2. In the left pane of the Model Explorer, select the model workspace for thesimulink_signal_localmodel.

      TheContentspane displays the data in the model workspace.

    3. Click theSimulink.SignalobjectA.

      In the right pane, make sure that the Model Explorer displays these attributes forA.

      Attribute Value
      Data type double
      Complexity real
      Dimensions 1
      Initial value 5

    See alsoModel Explorer.

  3. Double-click theMATLAB Functionblock to open its editor.

    TheMATLAB Functionblock modifies the value of global dataAeach time it executes.

    function y = fcn %#codegen global A; A = A+1; y = A;

  4. Make sure theSimulink.Signalobject is registered to theMATLAB Functionblock.

    1. In theModelingtab, in theDesignsection, clickSymbols Pane.

    2. In theSymbolspane, select the dataA. This data uses the same name as the global variable. Right click and selectInspect...to open the Property Inspector.

    3. 在属性检查器,设置Scopeof the data toData Store Memory.

  5. Simulate the model.

    TheMATLAB Functionblock reads the initial value of global data stored inAand updates the value ofAeach 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 theData Store Memoryblock. These diagnostics are available forData Store Memoryblocks only, not forSimulink.Signalobjects. For more information on using data store diagnostics, seeData Store Diagnostics.

Note

If you pass data store memory arrays to functions, optimizations such asA=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 noData Store Memoryblock support for:

  • MATLAB value classes

  • Variable-sized data

Related Topics