Share Data with Simulink and the MATLAB Workspace
Stateflow® charts interface with other blocks and objects in a Simulink® model by:
Sharing data through input and output connections.
Importing initial data values from the MATLAB® base workspace.
Saving final data values to the MATLAB base workspace.
Charts also can access Simulink parameters, data stores, and data dictionaries. For more information, see Share Parameters with Simulink and the MATLAB Workspace and Access Data Store Memory from a Chart.
Share Input and Output Data with Simulink
Data flows from Simulink into a Stateflow chart through input ports. Data flows from a Stateflow chart into Simulink through output ports.
To define input or output data in a chart:
Add a data object to the chart, as described in Add Stateflow Data.
Set the Scope property for the data object.
To define input data, set Scope to
Input Data
. An input port appears on the left side of the chart block.To define output data, set Scope to
Output Data
. An output port appears on the right side of the chart block.
By default, Port values appear in the order in which you add data objects. You can change these assignments by modifying the Port property of the data. When you change the Port property for an input or output data object, the Port values for the remaining input or output data objects automatically renumber.
Set the data type of the data object, as described in Specify Scope and Type of Stateflow Data.
Set the size of the data object, as described in Specify Size of Stateflow Data.
Note
You cannot set the type or size of Stateflow input data to accept frame-based data from Simulink.
Initialize Data from the MATLAB Base Workspace
You can import the initial value of a data symbol by defining it in the MATLAB base workspace and in the Stateflow hierarchy.
Define and initialize a variable in the MATLAB base workspace.
In the Stateflow hierarchy, define a data object with the same name as the MATLAB variable.
Set the Initialize method property for the data object to
Parameter
.
When the simulation starts, data resolution occurs. During this process, the Stateflow data object gets its initial value from the associated MATLAB variable.
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]
. Each element of the Stateflow array initializes to the same value as the corresponding element of the array in
the MATLAB base workspace.
The time of initialization depends on the data parent and scope of the Stateflow data object.
Data Parent | Scope | Initialization Time |
---|---|---|
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 entry |
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 |
Save Data to the MATLAB Base Workspace
At the end of simulation, a Stateflow chart that uses C as the action language can save the final value of a data object to the MATLAB base workspace.
Open the Model Explorer. In the Modeling tab, select Model Explorer.
Double-click the data object in the Contents pane.
In the Description pane of the Data properties dialog box, select Save final value to base workspace.
This option is available for data symbols of all scopes except
Constant
and Parameter
.