Sharing Data with Simulink® Models and the MATLAB® Workspace

Sharing Input and Output Data with Simulink® Models

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:

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

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

  3. Set the Scope property to one of these values:

    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.

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

  5. Decide if you want to use strong data typing with the Simulink model, as described in Strong Data Typing with Simulink® I/O.

  6. Set the size of the input or output data, as described in Sizing Stateflow® Data.

Sharing Simulink® Parameters with Stateflow® Charts

When to Share Simulink® Parameters

Share Simulink parameters with Stateflow charts to maintain consistency with your Simulink model.

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 Mask Editor in the Simulink software 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 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.

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

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS