| Stateflow® | ![]() |
| On this page… |
|---|
Sharing Input and Output Data with Simulink® Models Sharing Simulink® Parameters with Stateflow® Charts |
Data flows between Simulink® models and Stateflow® charts via input ports and output ports on the Stateflow chart block. The following example shows a Stateflow chart block with input and output data ports connected to the Simulink model.

To add input or output data to a Stateflow chart, follow these steps:
Add a data object to the Stateflow chart, as described in Adding Data Using the Stateflow® Editor.
Open the Data properties dialog, as described in Opening the Data Properties Dialog.
Set the Scope property to one of these values:
Input
This setting is the same as Input from Simulink in the Add > Data menu in the Stateflow Editor. A Simulink input port appears on the Stateflow chart block in the model.
Output
This setting is the same as Output to Simulink in the Add > Data menu in the Stateflow Editor. A Simulink output port appears on the Stateflow chart block in the model.
You assign inputs and outputs to ports in the order in which you add the data. For example, you assign the first input to input port 1 and the third output to output port 3. You can change port assignments by editing the value in the Port field of the Data properties dialog.
Set the type of the input or output data, as described in Typing Stateflow® Data.
Decide if you want to use strong data typing with the Simulink model, as described in Strong Data Typing with Simulink® I/O.
Set the size of the input or output data, as described in Sizing Stateflow® Data.
Note You cannot type or size Stateflow input data to accept frame-based data from a Simulink model. |
Share Simulink parameters with Stateflow charts to maintain consistency with your Simulink model.
To share Simulink parameters for a masked subsystem with a Stateflow chart, follow these steps:
In the Simulink mask editor for the parent subsystem, define and initialize a Simulink parameter (see Mask Editor in the Simulink software documentation).
In the Stateflow hierarchy, define a data object with the same name as the parameter (see Adding Data).
Set the scope of the data object to Parameter.
A Stateflow chart defines data of scope Parameter as a constant. You cannot change a parameter value during model execution.
When simulation starts, Simulink software attempts to resolve the Stateflow data object to a parameter at the lowest level masked subsystem. If unsuccessful, Simulink software moves up the model hierarchy to resolve the data object to a parameter at higher level masked subsystems.
You can initialize data from the MATLAB® base workspace. Initialization requires that you define data in both the MATLAB base workspace and the Stateflow hierarchy as follows:
Define and initialize a variable in the MATLAB workspace.
In the Stateflow hierarchy, define a data object with the same name as the MATLAB variable (see Adding Data).
Set the scope of the Stateflow data object to Parameter.
When simulation starts, data resolution occurs. During this process, the Stateflow data object gets its initial value from the associated MATLAB variable. For example, if the variable is an array, each element of the Stateflow array initializes to the same value as the corresponding element of the MATLAB array.
One-dimensional Stateflow arrays are compatible with MATLAB row and column vectors of the same size. For example, a Stateflow vector of size 5 is compatible with a MATLAB row vector of size [1,5] or column vector of size [5,1].
Data parent and scope control initialization time for Stateflow data objects.
Data Parent | Scope | When Initialized |
|---|---|---|
Machine | Local, Exported | Start of simulation |
Imported | Not applicable | |
Chart | Input | Not applicable |
Output, Local | Start of simulation or when chart reinitializes as part of an enabled Simulink subsystem | |
State with History Junction | Local | Start of simulation or when chart reinitializes as part of an enabled Simulink subsystem |
State without History Junction | Local | State activation |
Function (graphical, truth table, and Embedded MATLAB™ functions) | Input, Output | Function-call invocation |
Local | Start of simulation or when chart reinitializes as part of an enabled Simulink subsystem |
For all scopes except Constant and Parameter, you can instruct the Stateflow chart to save the final value of a data object at the end of simulation in the MATLAB base workspace (not as a masked subsystem parameter).
Use one of these techniques:
In the Value Attributes pane of the Data properties dialog, select the check box Save final value to base workspace.
In the Contents pane of the Model Explorer, follow these steps:
Select the row of the data object.
Select the check box in the SaveToWorkspace column.
![]() | Setting Data Properties in the Data Dialog | Sharing Global Data with Simulink® Models | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |