| Stateflow® | ![]() |
| On this page… |
|---|
You can share data between Stateflow charts in a single Stateflow machine by:
Defining data store memory objects that are parented by each Stateflow chart that wants to share the data.
Defining local data that are parented by the Stateflow machine
You can use data store memory objects to share data between selected charts in a single Stateflow machine. Follow these steps:
Use the Model Explorer to add a data object to each Stateflow chart that wants to share the data, 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.
Each data store memory object you add represents a common area of memory storage and functions as a global variable. You can extend the use of data store memory objects to share data with Simulink models, as described in Sharing Global Data with Simulink Models.
To share local data among all charts in a single Stateflow machine, follow these steps:
Use the Model Explorer to add a data object to the Stateflow machine in which the charts reside, as described in Adding Data Using the Model Explorer.
Set the scope of the data object to Local, as described in Setting Data Properties in the Data Dialog Box.
The new data object is visible to all charts in the parent Stateflow machine.
A Stateflow machine can share data with external modules, such as Stateflow charts in other machines or external code assigned to the machine. Sharing data requires that a Stateflow machine export the data definition to the external module and that the external module import the data definition from the Stateflow machine. Similarly, a Stateflow machine can import data that an external module exports.
To export data from the Stateflow machine to external modules, follow these steps:
In the Model Explorer, add a data object to the Stateflow machine, as described in Adding Data Using the Model Explorer.
When You Export Data to External Code Assigned to the Stateflow Machine. For each exported data object, the Stateflow code generator creates a C declaration of the form
type data;
where type is the C type of the exported data object — such as int16 or double — and data is the name of the Stateflow object. For example, suppose that your Stateflow machine defines an exported int16 item named counter. The Stateflow code generator exports the item as the C declaration
int16_T counter;
where int16_T is a defined type for int16 integers in Stateflow charts.
The code generator includes declarations for exported data in the generated target's global header file. This inclusion makes the declarations visible to external code compiled into or linked to the target.
See Exported Data for an example of Stateflow data exported to Stateflow external code.
When You Export Data to an External Stateflow Machine. For each Stateflow machine that wants to share the data exported from the external machine, you must define a data object of the same name as the exported data and set the object scope to Imported.
To import externally defined data into a Stateflow machine, follow these steps:
In the Model Explorer, add a data object to the Stateflow machine, as described in Adding Data Using the Model Explorer.
Give the data object the same name as the external data.
When You Import Data from External Code Assigned to the Stateflow Machine. For each imported data object, the Stateflow code generator assumes that external code provides a prototype of the form
type data;
where type is the C data type corresponding to the Stateflow data type of the imported item — such as int32 or double — and data is the name of the Stateflow object. For example, suppose that your Stateflow machine defines an imported int32 integer named counter. The Stateflow code generator expects the item to be defined in the external C code as
int32_T counter;
See Imported Data for an example of Stateflow external code data imported into the Stateflow machine.
When You Import Data from an External Stateflow Machine. Make sure that the external Stateflow machine contains a data definition of scope Exported with the same name as the imported data objects.
![]() | Sharing Global Data with Simulink Models | Typing Stateflow Data | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |