| Contents | Index |
| 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.
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.
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:

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 Best Practices for Using Data Stores in Stateflow Charts.
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.
In the Model Explorer, follow these steps:
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 Best Practices 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.
To create read/write references to a global data store that you can share across multiple models:
Define data store memory objects that reside in each chart that shares the data.
Use the Model Explorer to add a data object to each chart, as described in Adding Data Using the Model Explorer.
Give each data object the same name.
Set the scope of each data object to Data Store Memory.
Verify that your models do not contain any Data Store Memory blocks.
However, you can include Data Store Read and Data Store Write blocks.
Create a Simulink.Signal object in the MATLAB base workspace.
In the Model Explorer, navigate to Simulink Root > Base Workspace in the Model Hierarchy pane.
Select Add > Simulink.Signal.
Give the object the same name as the data store memory objects in your charts.
Verify that these settings apply to the Simulink.Signal object:
Set Data type to an explicit data type.
The data type cannot be auto.
Set Dimensions to be fully specified.
The signal dimensions cannot be –1, or inherited.
Set Complexity to real.
Set Sample mode to Sample based.
Set Storage class to ExportedGlobal.
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 the signal type as real.
Specify a data type other than auto.
Minimize the use of automatic-mode properties.
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 Controlling and Displaying the Sorted Order in the Simulink documentation.
![]() | Sharing Inputs, Outputs, and Parameters with Simulink and the MATLAB Workspace | Sharing Chart Data with External Modules | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |