Main Content

Access Bus Signals Through Stateflow Structures

A Stateflow® structure is a data type that you define from a Simulink.Bus (Simulink) object. Using Stateflow structures, you can bundle data of different size and type to create:

  • Inputs and outputs that access Simulink® bus signals from Stateflow charts, Truth Table blocks, and MATLAB Function blocks.

  • Local data in Stateflow charts, truth tables, graphical functions, MATLAB® functions, and boxes.

  • Temporary data in Stateflow graphical functions, truth tables, and MATLAB functions.

For more information, see Create Simulink Bus Objects (Simulink).

Example of Stateflow Structures

In this example, a Stateflow chart receives a bus input signal by using the structure inbus and outputs a bus signal from the structure outbus. The input signal comes from the Simulink Bus Creator block COUNTERBUSCreator, which bundles signals from two other Bus Creator blocks. The output structure outbus connects to a Simulink Bus Selector block. Both inbus and outbus derive their type from the Simulink.Bus object COUNTERBUS. For more information about this example, see Integrate Custom Structures in Stateflow Charts.

The elements of a Stateflow structure data type are called fields. Fields can be any combination of individual signals, muxed signals, vectors, and other structures (also called substructures). Each field has its own data type. The data type does not have to match the type of any other field in the structure. For example, in this model, each of the structures inbus and outbus has two fields:

  • inputsignal is a substructure with one field, input.

  • limits is a substructure with two fields, upper_saturation_limit and lower_saturation_limit.

Define Stateflow Structures

  1. To define the structure data type, create a Simulink bus object in the base workspace, as described in Create Simulink Bus Objects (Simulink).

  2. Add a data object to the chart, as described in Add Stateflow Data.

    To define temporary structures in truth tables, graphical functions, and MATLAB functions, add a data object to your function. For more information, see Add Data Through the Model Explorer.

  3. Set the Scope property for the structure. Your choices are:

    • Input

    • Output

    • Local

    • Parameter

    • Data Store Memory

    • Temporary (Only in charts that use C as the action language)

  4. Set the Type property for the structure. Depending on its scope, a Stateflow structure can have one of these data types.

    Inherit: Same as Simulink

    This option is available for input structures only. The input structure inherits its data type from the Simulink bus signal in your model that connects to it. The Simulink bus signal must be a nonvirtual bus. For more information, see Virtual and Nonvirtual Buses.

    In the base workspace, specify a Simulink.Bus object with the same properties as the bus signal that connects to the Stateflow input structure. These properties must match:

    • Number, name, and type of inputs

    • Dimension

    • Sample Time

    • Complexity

    • Sampling Mode

    If the input signal comes from a Bus Creator block, in the Bus Creator dialog box, specify an appropriate bus object for Output data type field. 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>

    In the Type field, replace <object name> with the name of the Simulink.Bus object that defines the Stateflow structure.

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

    <date type expression>

    In the Type field, replace <data type expression> with an expression that evaluates to a data type. For example:

For example, in the sf_bus_demo model, the input structure inbus and the output structure outbus derive their type through a type specification of the form Bus: COUNTERBUS.

Model Explorer showing the specification for the data object inbus.

Specify Structure Types by Calling the type Operator

To specify structure types, you can use expressions that call the Stateflow type operator. This operator sets the type of one structure to the type of another structure in the Stateflow chart. For example, in the sf_bus_demo model, a type operator expression specifies the type of the local structure counterbus_struct in terms of the input structure inbus. Both structures are defined from the Simulink.Bus object COUNTERBUS. For more information, see Derive Data Types from Other Data Objects.

Model Explorer showing the specification for the data object counterbus_struct.

Virtual and Nonvirtual Buses

Simulink models support virtual and nonvirtual buses. Nonvirtual buses read their inputs from data structures stored in contiguous memory. Virtual buses read their inputs from noncontiguous memory. For more information, see Composite Interface Guidelines (Simulink).

Stateflow charts support only nonvirtual buses. Stateflow input structures can accept virtual bus signals and convert them to nonvirtual bus signals. Stateflow input structures cannot inherit properties from virtual bus signals. If the input to a chart is a virtual bus, set the Type property of the input structure through a type specification of the form Bus: <object name>.

Debug Structures

To debug a Stateflow structure, open the Stateflow Breakpoints and Watch window and examine the values of structure fields during simulation. To view the values of structure fields at the command line, use dot notation to index into the structure. For more information, see Inspect and Modify Data and Messages While Debugging.

Guidelines for Structure Data Types

  • Define each structure from a Simulink.Bus object in the base workspace.

  • Structures cannot have a constant scope.

  • Structures of parameter scope must be tunable.

Bus Limitations for Custom Code

You cannot import a bus defined in custom code if that bus contains multidimensional arrays.

To use buses containing multidimensional arrays in custom code, define the bus in Stateflow and pass the bus to your custom code.

For more information about defining and passing buses in custom code, see Integrate Custom Structures in Stateflow Charts.

See Also


Related Topics