| Contents | Index |
| On this page… |
|---|
Sharing Input Data with Simulink Sharing Output Data with Simulink Sharing Simulink Parameters with Charts |
Data flows from Simulink into a chart via input ports on the Stateflow chart block.
To add input data to a chart:
Add a data object to the chart, as described in Adding Data Using the Stateflow Editor.
Open the Data properties dialog box, as described in Opening the Data Properties Dialog Box.
Set the Scope property to Input.
An input port appears on the Stateflow chart block in the model.
You assign inputs to ports in the order in which you add the data. For example, you assign the second input to input port 2. You can change port assignments by editing the value in the Port field of the Data properties dialog box.
Set the type of the input data, as described in Typing Stateflow Data.
Set the size of the input data, as described in Sizing Stateflow Data.
Data flows from a chart into Simulink via output ports on the Stateflow chart block.
To add output data to a chart:
Add a data object to the chart, as described in Adding Data Using the Stateflow Editor.
Open the Data properties dialog box, as described in Opening the Data Properties Dialog Box.
Set the Scope property to Output.
An output port appears on the Stateflow chart block in the model.
You assign outputs to ports in the order in which you add the data. For example, you assign 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 box.
Set the type of the output data, as described in Typing Stateflow Data.
Set the size of the output data, as described in Sizing Stateflow Data.
Share Simulink parameters with Stateflow charts to maintain consistency with your Simulink model. By using parameters, you can also avoid hard-coding data sizes and types.
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 Working with Block Masks in the Simulink 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 chart defines data of scope Parameter as a constant. You cannot change a parameter value during model execution.
When simulation starts, Simulink tries to resolve the Stateflow data object to a parameter at the lowest level masked subsystem. If unsuccessful, Simulink 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 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 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 Description pane of the Data properties dialog box, select 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 Box | Sharing Global Data with Multiple Charts | ![]() |

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 |