Access Data Store Memory from a Chart - MATLAB & Simulink (original) (raw)

A Simulink® model implements global variables as data stores, either asData Store Memory (Simulink) blocks or as instances of Simulink.Signal (Simulink) 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. Stateflow can access data stores in Simulink that have unbounded dimensions.

Local and Global Data Store Memory

Stateflow charts can interface with local and global data stores.

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) andData 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

Specify the Data Store Properties

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:

Share Data Store Memory Across Multiple Models

To access a global data store from multiple models:

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:

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

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

Block parameters dialog box for a Data Store Memory block.

Do Not Map Data Store Subelements to Atomic Subcharts and Boxes

Data Store Memory blocks do not support mapping non-trivial expressions to atomic subcharts and boxes. For example, if a data store contains a bus, you can map the bus, but not a specific field of the bus. For more information about mapping, see Map Variables for Atomic Subcharts and Boxes.

See Also

Simulink.Signal (Simulink) | Data Store Memory (Simulink) | Data Store Read (Simulink) | Data Store Write (Simulink)

Topics