Main Content

Add Stateflow Data

When you want to store values that are visible at a specific level of the Stateflow® hierarchy, add data to your chart. When you simulate your model, chart data objects retain their values for the duration of the simulation.

Data defined in a Stateflow chart is visible by multiple Stateflow objects in the chart, including states, transitions, MATLAB® functions, and truth tables. To determine what data is used in a state or transition, right-click the state or transition and select Explore. A context menu lists the names and scopes of all resolved symbols in the state or transition. Selecting a symbol from the context menu displays its properties in the Model Explorer. Selecting an output event from the context menu opens the Simulink® subsystem or Stateflow chart associated with the event.

Note

Stateflow data is not available to Simulink functions within a Stateflow chart.

You can add data to a Stateflow chart by using the Symbols pane, the Stateflow Editor menu, or the Model Explorer.

Add Data Through the Symbols Pane

  1. In the Modeling tab, under Design Data, select Symbols Pane.

  2. Click the Create Data icon .

  3. In the row for the new data, under Type, click the icon and choose:

    • Input Data

    • Local Data

    • Output Data

    • Constant

    • Data Store Memory

    • Parameter

    • Temporary

    For more information about these options, see Scope.

  4. Edit the name of the data.

  5. For input and output data, click the Port field and choose a port number.

  6. To specify properties for data, open the Property Inspector. In the Symbols pane, right-click the row for the symbol and select Explore. For more information, see Set Data Properties.

Add Data by Using the Stateflow Editor Menu

  1. In a Stateflow chart in a Simulink model, select the menu option corresponding to the scope of the data that you want to add. For more information about these options, see Scope.

    ScopeMenu Option
    Input

    In the Modeling tab, under Design Data, select Data Input.

    Output

    In the Modeling tab, under Design Data, select Data Output.

    Local

    In the Modeling tab, under Design Data, select Local.

    Constant

    In the Modeling tab, under Design Data, select Constant.

    Parameter

    In the Modeling tab, under Design Data, select Parameter.

    Data Store Memory

    In the Modeling tab, under Design Data, select Data Store.

  2. In the Data dialog box, specify data properties. For more information, see Set Data Properties.

Add Data Through the Model Explorer

To add function- or state-parented data to Stateflow charts in Simulink models, use the Model Explorer:

  1. In the Modeling tab, under Design Data, select Model Explorer.

  2. In the Model Hierarchy pane, select the object in the Stateflow hierarchy where you want to make the new data visible. The object that you select becomes the parent of the new data.

  3. In the Model Explorer toolstrip, select the Add Data button. Alternatively, in the Model Explorer menu, select Add > Data. The new data with a default definition appears in the Contents pane of the Model Explorer.

  4. In the Data pane, specify the properties of the data. For more information, see Set Data Properties.

Tip

You do not need to create local or temporary data explicitly in these types of functions:

  • Graphical functions in charts that use MATLAB as the action language

  • Truth table functions that use MATLAB as the action language

  • MATLAB functions

Instead, in these functions, you can use undefined variables to store values that are accessible only during the rest of the function call. To store values that persist across function calls, use local data at the chart level. Alternatively, in MATLAB functions, you can use the keyword persistent.

Best Practices for Using Data in Charts

Avoid Inheriting Output Data Properties from Simulink Blocks

Stateflow output data should not inherit properties from output signals, because the values back propagate from Simulink blocks and can be unpredictable.

Generate More Efficient Code by Using In-Place Data

You can improve the performance and decrease the memory footprint of the generated code for your Stateflow charts, truth tables, and state transition tables by using in-place data. You create in-place data when you use the same data name for a chart input and a chart output. When you generate code from the chart, the generated code treats the input and output data as a single in-place argument passed by reference. Using in-place data reduces the number of times that the generated code copies intermediate data, which results in more efficient code.

When input and output data have the same name, you can edit properties only for the input data. The properties for the output data are read-only.

Related Topics