Share Data with Simulink and MATLAB Workspace

Share Input Data with Simulink

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

To add input data to a chart:

  1. Add a data object to the chart, as described in Add 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 Open the Data Properties Dialog Box.

  3. Set the Scope property to Input.

    An input port appears on the 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 Type Stateflow Data.

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

    Note:   You cannot type or size Stateflow® input data to accept frame-based data from Simulink.

Share Output Data with Simulink

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

To add output data to a chart:

  1. Add a data object to the chart, as described in Add 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 Open the Data Properties Dialog Box.

  3. Set the Scope property to Output.

    An output port appears on the 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 Type Stateflow Data.

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

Share Simulink Parameters with Charts

When to Share Simulink Parameters

Share Simulink parameters with charts to maintain consistency with your Simulink model. By using parameters, you can also avoid hard-coding data sizes and types.

Parameters defined in a Stateflow chart can be used by multiple Stateflow objects in the chart, such as states, MATLAB® functions and truth tables.

How to Share Simulink Parameters

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

  1. In the Simulink mask editor for the parent subsystem, define and initialize a Simulink parameter.

  2. In the Stateflow hierarchy, define a data object with the same name as the parameter (see Add 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.

Initialize 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 Add 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

Save 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:

  • 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:

    1. Select the row of the data object.

    2. Select the check box in the SaveToWorkspace column.

Was this topic helpful?