Skip to Main Content Skip to Search
Product Documentation

Defining Stateflow Structures

Rules for Defining Structure Data Types in Charts

Follow these rules when defining structures in Stateflow charts:

Defining Structure Inputs and Outputs

Interfacing Stateflow Structures with Simulink Bus Signals

You can drive Stateflow structure inputs by using any Simulink bus signal that has matching properties. Similarly, Stateflow charts can output structures to Simulink blocks that accept bus signals.

To create inputs and outputs in Stateflow charts:

  1. Create a Simulink bus object in the base workspace to define the structure type for your Stateflow chart.

    For information about how to create Simulink bus objects, see Simulink.Bus in the Simulink Reference documentation.

  2. Select Tools > Explore in the Stateflow Editor to open the Model Explorer.

  3. In the Model Explorer, add a data object as described in Adding Data Using the Model Explorer.

    The Model Explorer adds a data object and opens a Properties dialog box in its right-hand Dialog pane.

  4. In the Name field of the Properties dialog box, enter the name of the structure data.

  5. In the Scope field, select either Input or Output.

  6. In the Type field, select Inherit: Same as Simulink, Bus: <object name>, or <data type expression> according to these guidelines:

    TypeWorks with ScopeRequirements
    Inherit: Same as SimulinkInput

    You do not need to specify a value. The data type is inherited from previously-defined data, based on the scope you selected for the data object.

    There must be a Simulink bus signal in your model that connects to the Stateflow structure input.

    The Simulink bus signal must be a nonvirtual bus (see Working with Virtual and Nonvirtual Buses).

    You must specify a Simulink.Bus object in the base workspace with the same properties as the bus signal in your model that connects to the Stateflow structure input. The following properties must match:

    • Number, name, and type of inputs

    • Dimension

    • Sample Time

    • Complexity

    • Sampling Mode

    If your input signal comes from a Bus Creator block, you must specify an appropriate bus object for Output data type in the Bus Creator dialog box. When you specify the bus object, Simulink verifies that the properties of the Simulink.Bus object in the base workspace match the properties of the Simulink bus signal.

    Bus: <object name>Input or Output

    Replace "<object name>" in the Type field with the name of the Simulink.Bus object in the base workspace that defines the Stateflow structure. For example: Bus: inbus.

      Note   You are not required to specify a bus signal in your Simulink model that connects to the Stateflow structure input or output. However, if you do specify a bus signal, its properties must match the Simulink.Bus object that defines the Stateflow structure input or output.

    <date type expression>Input or OutputReplace "<data type expression>" in the Type field with an expression that evaluates to a data type.

    Enter the expression according to these guidelines:

    • For structure inputs, you can use the Stateflow type operator to assign the type of your structure based on the type of another structure defined in the Stateflow chart, as described in Defining Structure Types with Expressions.

        Note   You cannot use the type operator for structure outputs (structures of scope Output).

    • For structure inputs or outputs, you can enter the name of the Simulink.Bus object in the base workspace that defines the Stateflow structure.

  7. Click Apply.

Working with Virtual and Nonvirtual Buses

Simulink models support virtual and nonvirtual buses. Virtual buses read their inputs from noncontiguous memory, while nonvirtual buses read their inputs from data structures stored in contiguous memory (see Virtual and Nonvirtual Buses in the Simulink documentation).

Stateflow charts support nonvirtual buses only. When Simulink models contain Stateflow structure inputs and outputs, a hidden converter block converts bus signals for use with Stateflow charts, as follows:

Even though this conversion process allows Stateflow charts to accept virtual and nonvirtual buses as input, Stateflow structures cannot inherit properties from virtual bus input signals. If the input to a chart is a virtual bus, you must set the data type mode of the Stateflow bus input to Bus Object, as described in Interfacing Stateflow Structures with Simulink Bus Signals.

Defining Local Structures

To define local structures:

  1. Create a Simulink bus object in the base workspace to define the structure type for your Stateflow chart.

    For information about how to create Simulink bus objects, see Simulink.Bus in the Simulink Reference documentation.

  2. Select Tools > Explore in the Stateflow Editor to open the Model Explorer.

  3. In the Model Explorer, add a data object as described in Adding Data Using the Model Explorer.

    The Model Explorer adds a data object and opens a Properties dialog box in its right-hand Dialog pane.

  4. In the Name field of the Properties dialog box, enter the name of the structure data.

  5. In the Scope field, select Local.

  6. In the Type field, select either Bus: <object name>, or <data type expression>, and then specify the expression as follows:

    TypeWhat to Specify
    Bus: <object name>

    Replace "<object name>" in the Type field with the name of the Simulink.Bus object in the base workspace that defines the Stateflow structure. For example: Bus: inbus.

    <date type expression>Replace "<data type expression>" in the Type field with an expression that evaluates to a data type.

    You can enter any of the following expressions:

    • Use the Stateflow type operator to assign the type of your structure based on the type of another structure defined in the Stateflow chart, as described in Defining Structure Types with Expressions

    • Enter the name of the Simulink.Bus object in the base workspace that defines the Stateflow structure.

  7. Click Apply.

Defining Structures of Parameter Scope

To define structures of parameter scope:

  1. Create a Simulink bus object in the base workspace to define the structure type for your chart.

    For information about how to create Simulink bus objects, see Simulink.Bus in the Simulink Reference documentation.

  2. Select Tools > Explore in the Stateflow Editor to open the Model Explorer.

  3. In the Model Explorer, add a data object as described in Adding Data Using the Model Explorer.

    The Model Explorer adds a data object and opens a Properties dialog box in its right-hand Dialog pane.

  4. In the Name field of the Properties dialog box, enter the name of the structure data.

  5. In the Scope field, select Parameter.

  6. In the Type field, select either Bus: <object name>, or <data type expression>, and then specify the expression as follows:

    TypeWhat to Specify
    Bus: <object name>

    Replace "<object name>" in the Type field with the name of the Simulink.Bus object in the base workspace that defines the Stateflow structure. For example: Bus: inbus.

    <date type expression>Replace "<data type expression>" in the Type field with an expression that evaluates to a data type.

    You can enter any of the following expressions:

    • Use the Stateflow type operator to assign the type of your structure based on the type of another structure defined in the Stateflow chart, as described in Defining Structure Types with Expressions

    • Enter the name of the Simulink.Bus object in the base workspace that defines the Stateflow structure.

  7. Click Apply.

Defining Temporary Structures

You can define temporary structures in truth tables, graphical functions, and MATLAB functions of a Stateflow chart.

To define a temporary structure:

  1. Create a Simulink bus object in the base workspace to define the structure type for your chart.

    For information about how to create Simulink bus objects, see Simulink.Bus in the Simulink Reference documentation.

  2. Select Tools > Explore in the Stateflow Editor to open the Model Explorer.

  3. In the Model Explorer, add a data object to your function as described in Adding Data Using the Model Explorer.

    The Model Explorer adds a data object and opens a Properties dialog box in its right-hand Dialog pane.

  4. In the Name field of the Properties dialog box, enter the name of the structure data.

  5. In the Scope field, select Temporary.

  6. In the Type field, select either Bus: <object name>, or <data type expression>, and then specify the expression as follows:

    TypeWhat to Specify
    Bus: <object name>

    Replace "<object name>" in the Type field with the name of the Simulink.Bus object in the base workspace that defines the Stateflow structure. For example: Bus: inbus.

    <date type expression>Replace "<data type expression>" in the Type field with an expression that evaluates to a data type.

    You can enter any of the following expressions:

    • Use the Stateflow type operator to assign the type of your structure based on the type of another structure defined in the Stateflow chart, as described in Defining Structure Types with Expressions

    • Enter the name of the Simulink.Bus object in the base workspace that defines the Stateflow structure.

  7. Click Apply.

Defining Structure Types with Expressions

You can define structure types with expressions that call the Stateflow type operator. This operator assigns the type of your structure based on the type of another structure defined in the Stateflow chart. For example, the model sfbus_demo contains a local structure whose type is defined using a type operator expression, as follows:

In this case, the structure counterbus_struct derives its type from structure inbus, which is defined by the Simulink.Bus object COUNTERBUS. Therefore, the structure counterbus_struct is also defined by the bus object COUNTERBUS.

To learn how to use the Stateflow type operator, see Deriving Data Types from Previously Defined Data.

  


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