Access Data Store Memory from a Chart

A Simulink® model implements global variables as data stores, either as Data Store Memory blocks or as instances of Simulink.Signal objects. You can use data stores to share data between multiple Simulink blocks without explicit input or output 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.

To access global data from a chart, bind a Stateflow data object to a Simulink data store. After you create the binding, the Stateflow data object becomes a symbolic representation of the Simulink data store memory. You can then use this symbolic object to store and retrieve global data.

Local and Global Data Store Memory

Stateflow charts can interface with local and global data stores.

  • Local data stores are visible to all blocks in one model. To interact with a local data store, a chart must reside in the model where you define the local data store. You can define a local data store by adding a Data Store Memory block to a model or by creating a Simulink signal object.

  • Global data stores have a broader scope that crosses model reference boundaries. To interact with global data stores, a chart must reside in the top model where you define the global data store or in a model that the top model references. You implement global data stores as Simulink signal objects.

For more information, see Local and Global Data Stores (Simulink).

Bind Stateflow Data to Data Stores

  1. To define the Simulink data store memory, add a Data Store Memory block to your model or create a Simulink signal object. For more information, see Data Stores with Data Store Memory Blocks (Simulink) and Data Stores with Signal Objects (Simulink).

  2. Add a data object to the Stateflow chart, as described in Add Stateflow Data.

  3. Set the Name property as the name of the Simulink data store memory to which you want to bind the Stateflow data object.

  4. Set the Scope property to Data Store Memory.

The Stateflow data object inherits all additional properties from the data store memory to which you bind the object.

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.

Store and Retrieve Global Data

After binding a Stateflow data object to a Simulink data store, you can store and retrieve global data in state and transition actions. The data object acts as a global variable that you reference by its symbolic name. When you store numeric values in this variable, you are writing to the Simulink data store memory. When you retrieve numeric values from this variable, you are reading from the data store memory.

For example, in this chart, the state actions read from and write to a Data Store Memory block called myglobal.

Best Practices for Using Data Stores

Data Store Properties in Charts

When you bind a Stateflow data object to a data store, the Stateflow object inherits all of its properties from the data store. To ensure that properties propagate correctly, when you create the Simulink data stores:

  • Specify a data type other than auto.

  • Minimize the use of automatic-mode properties.

Share Data Store Memory Across Multiple Models

To access a global data store from multiple models:

  • Verify that your models do not contain any Data Store Memory blocks. You can include Data Store Read and Data Store Write blocks.

  • In the MATLAB® base workspace, create a Simulink.Signal object with these attributes:

    • Set Data type to an explicit data type. The data type cannot be Auto.

    • Fully specify Dimensions. The signal dimensions cannot be –1 or Inherited.

    • Fully specify Complexity. The complexity cannot be Auto.

    • Set Storage class to ExportedGlobal.

  • In each chart that shares the data, bind a Stateflow data object to the Simulink data store.

Write to Data Store Memory Before Reading

To avoid algorithm latency, write to data store memory before reading from it. Otherwise, the read actions retrieve the value that was stored in the previous time step, rather than the value computed and stored in the current time step. When unconnected blocks share global data while running at different rates:

  • Segregate read actions into separate blocks from write actions.

  • Assign priorities to blocks so that your model invokes write blocks before read blocks. For more information, see Control and Display the Sorted Order (Simulink).

To avoid situations when multiple reads and writes occur unintentionally in the same time step, enable the Data Store Memory block diagnostics to:

  • Detect Read Before Write

  • Detect Write After Read

  • Detect Write After Write

If you use a data store memory block as a persistent global storage area for accumulating values across time steps, avoid unnecessary warnings by disabling the Data Store Memory block diagnostics. For more information, see Data Store Diagnostics (Simulink).

See Also

| | |

Related Topics