Documentation Center

  • Trial Software
  • Product Updates

Share Global Data with Multiple Charts

About Data Stores

You can use an interface to direct charts to access global variables in Simulink® models. A Simulink model implements global variables as data stores, created either as data store memory blocks or as instances of Simulink.Signal objects. Data stores enable multiple Simulink blocks to share data without the need for explicit I/O connections to pass data from one block to another. Stateflow® charts share global data with Simulink models by reading from and writing to data store memory symbolically.

You can use data stores with buses, but not with arrays of buses. For more information about using data stores with buses, see "Using Data Stores with Buses and Arrays of Buses" in the Simulink documentation.

How Charts Work with Local and Global Data Stores

Charts can interface with local and global data stores. Local data stores, often implemented as data store memory blocks, are visible to all blocks in one model. To interact with local data stores, a chart must reside in the modelmodel where you define the local data store:

Global data stores have a broader scope, which crosses model reference boundaries. To interact with global data stores, a chart must reside either in the top model — where the global data store is defined — or in any model that the top model references. You implement global data stores as Simulink signal objects.

Access Data Store Memory from a Chart

To access global data in a Simulink model from a chart, you must bind a Stateflow data object to a Simulink data store — either a data store memory block or a signal object (see Bind a Stateflow Data Object to Data Store Memory). After you create the binding, the Stateflow data object becomes a symbolic representation of Simulink data store memory. You can then use this symbolic object to store and retrieve global data (see Read and Write Global Data Programmatically).

Bind a Stateflow Data Object to Data Store Memory

To bind a Stateflow data object to Simulink data store memory, you must create a data object in the Stateflow hierarchy with the same name as the data store and with scope set to Data Store Memory. The Stateflow data object inherits all properties from the data store to which you bind the object. Follow guidelines for specifying data store properties in Best Practices for Using Data Stores in Charts.

    Note:   You cannot edit properties that the data object inherits from the data store.

Use the Stateflow Editor to Bind a Data Object

In the Stateflow Editor, follow these steps:

  1. Select Chart > Add Other Elements > Data Store Memory.

    The properties dialog box for the new data object appears with scope property set to Data Store Memory.

  2. In the Name field of the Data properties dialog box, enter the name of the Simulink data store to which you want to bind.

  3. Click OK.

Use the Model Explorer to Bind a Data Object

In the Model Explorer, follow these steps:

  1. Select Add > Data.

    The Model Explorer adds a data object to the chart.

  2. Double-click the new data object to open its properties dialog box, and enter the following information in the General pane:

    FieldWhat to Specify
    NameEnter the name of the Simulink data store memory block to which you want to bind.
    ScopeSelect Data Store Memory from the drop-down menu.

  3. Click OK.

Resolve Data Store Bindings

Multiple local and global data stores with the same name can exist in the same model hierarchy. In this situation, the Stateflow data object binds to the data store that is the nearest ancestor.

Read and Write Global Data Programmatically

You can use the Stateflow data object that you bind to Simulink data store memory to store and retrieve global data in states and transitions. Think of this object as a global variable that you reference by its symbolic name — the same name as the data store to which you bind the object. When you store numeric values in this variable, you are writing to Simulink data store memory. Similarly, when you retrieve numeric values from this variable, you are reading from the data store memory.

The following chart reads from and writes to a data store memory block called myglobal.

Diagnostics for Sharing Data Between Charts and Simulink Blocks

Errors to Check For

Multiple reads and writes can occur unintentionally in the same time step. To detect these situations, you can configure data store memory blocks to generate errors or warnings for these conditions:

  • Read before write

  • Write after write

  • Write after read

    Note:   These diagnostics are available only for data store memory blocks used within a single Simulink model, not for data stores created from Simulink signal objects. In other words, these diagnostics do not work for global data stores that cross model reference boundaries.

When to Enable Diagnostics

Enable diagnostics on data store memory blocks to ensure the validity of data that multiple unconnected blocks share while running at different rates. In this scenario, you can detect conditions when writes do not occur before reads in the same time step. To prevent these violations, see Best Practices for Using Data Stores in Charts.

When to Disable Diagnostics

If you use a data store memory block as a persistent global storage area for accumulating values across time steps, disable diagnostics to avoid generating unnecessary warnings.

How to Set Diagnostics for Shared Data

To set diagnostics on data store memory blocks, follow these steps:

  1. Double-click the data store memory block in your Simulink model to open its Block Parameters dialog box.

  2. Click the Diagnostics tab.

  3. Enable diagnostics by selecting warning or error from the drop-down menu for each condition you want to detect.

  4. Click OK.

Create a Global Data Store Across Multiple Models

To create read/write references to a global data store that you can share across multiple models:

  1. Define data store memory objects that reside in each chart that shares the data.

    1. Use the Model Explorer to add a data object to each chart, as described in How to Add Data Using the Model Explorer.

    2. Give each data object the same name.

    3. Set the scope of each data object to Data Store Memory.

  2. Verify that your models do not contain any Data Store Memory blocks.

    However, you can include Data Store Read and Data Store Write blocks.

  3. Create a Simulink.Signal object in the MATLAB® base workspace.

    1. In the Model Explorer, navigate to Simulink Root > Base Workspace in the Model Hierarchy pane.

    2. Select Add > Simulink Signal.

    3. Give the object the same name as the data store memory objects in your charts.

  4. Verify 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 Sample mode to Sample based.

    4. Set Storage class to ExportedGlobal.

Best Practices for Using Data Stores in Charts

When Binding to Data Stores in Charts

When you bind a Stateflow data object to a data store, the Stateflow object inherits all properties from the data store. To ensure that properties propagate correctly when you access data stores, follow these guidelines to create data stores:

  • Specify a data type other than auto.

  • Minimize the use of automatic-mode properties.

When Enforcing Writes Before Reads in Unconnected Blocks

To enforce writes before reads when unconnected blocks share global data in charts, follow these guidelines:

  • Segregate reads into separate blocks from writes.

  • Assign priorities to blocks so that your model invokes write blocks before read blocks.

    For instructions on how to set block execution order, see Control and Display the Sorted Order in the Simulink documentation.

Was this topic helpful?