| Stateflow® | ![]() |
| On this page… |
|---|
How Stateflow Charts Work with Local and Global Data Stores Accessing Data Store Memory from a Stateflow Chart Diagnostics for Sharing Data Between Stateflow Charts and Simulink Blocks |
You can use an interface to direct Stateflow 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 using Stateflow action language.
Stateflow 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 Stateflow chart must reside in the model where you define the local data store, as shown below.

Global data stores have a broader scope, which crosses model reference boundaries. To interact with global data stores, a Stateflow 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.
To access global data in a Simulink model from a Stateflow chart, you must bind a Stateflow data object to a Simulink data store — either a data store memory block or a signal object (see Binding 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 using Stateflow action language (see Reading and Writing Global Data Programmatically).
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 Tips for Using Data Stores in Stateflow Charts.
This properties dialog box shows a Stateflow data object that you bind to a data store.

Note You cannot edit properties that the data object inherits from the data store. |
In the Stateflow Editor, follow these steps:
Select Add > Data > Data Store Memory.
The properties dialog box for the new data object appears with scope property set to Data Store Memory.
In the Name field of the Data properties dialog box, enter the name of the Simulink data store to which you want to bind.
Click OK.
To use the Model Explorer, follow these steps:
In the Stateflow Editor, select Tools > Explore.
The Model Explorer appears.
In the Model Explorer, select Add > Data.
The Model Explorer adds a data object to the Stateflow chart.
Double-click the new data object to open its properties dialog box, and enter the following information in the General pane:
| Field | What to Specify |
|---|---|
| Name | Enter the name of the Simulink data store memory block to which you want to bind. |
| Scope | Select Data Store Memory from the drop-down menu. |
Click OK.
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.
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 using Stateflow action language. 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.
This example of Stateflow action language reads from and writes to a data store memory block called myglobal.

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. |
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 Tips for Using Data Stores in Stateflow Charts.
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.
To set diagnostics on data store memory blocks, follow these steps:
Double-click the data store memory block in your Simulink model to open its Block Parameters dialog box.
Click the Diagnostics tab.
Enable diagnostics by selecting warning or error from the drop-down menu for each condition you want to detect.
Click OK.
![]() | Sharing Data with Simulink Models and the MATLAB Workspace | Sharing Data Between Charts and with External Modules | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |