Skip to Main Content Skip to Search
Product Documentation

Sharing Inputs, Outputs, and Parameters with Simulink and the MATLAB Workspace

Sharing Input Data with Simulink

Data flows from Simulink into a chart via input ports on the Stateflow chart block.

To add input data to a chart:

  1. Add a data object to the chart, as described in Adding Data Using the Stateflow Editor.

      Note   Add the data to the chart itself, not to any other object in the chart.

  2. Open the Data properties dialog box, as described in Opening the Data Properties Dialog Box.

  3. 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.

  4. Set the type of the input data, as described in Typing Stateflow Data.

  5. Set the size of the input data, as described in Sizing Stateflow Data.

Sharing Output Data with Simulink

Data flows from a chart into Simulink via output ports on the Stateflow chart block.

To add output data to a chart:

  1. Add a data object to the chart, as described in Adding Data Using the Stateflow Editor.

      Note   Add data to the chart itself, not to any other object in the chart.

  2. Open the Data properties dialog box, as described in Opening the Data Properties Dialog Box.

  3. 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.

  4. Set the type of the output data, as described in Typing Stateflow Data.

  5. Set the size of the output data, as described in Sizing Stateflow Data.

Sharing Simulink Parameters with Charts

When to Share Simulink Parameters

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.

How to Share Simulink Parameters

To share Simulink parameters for a masked subsystem with a Stateflow chart, follow these steps:

  1. In the Simulink mask editor for the parent subsystem, define and initialize a Simulink parameter. See Working with Block Masks in the Simulink documentation.

  2. In the Stateflow hierarchy, define a data object with the same name as the parameter (see Adding Data).

  3. 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.

Initializing Data from the MATLAB Base Workspace

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:

  1. Define and initialize a variable in the MATLAB workspace.

  2. In the Stateflow hierarchy, define a data object with the same name as the MATLAB variable (see Adding Data).

  3. 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].

Time of Initialization

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

Saving Data to the MATLAB Workspace

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:

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS