Index and Assign Values to Stateflow Structures - MATLAB & Simulink (original) (raw)

This example shows how to access and modify the contents of a Stateflow® structure or an array of Stateflow structures. A Stateflow structure is a data type that you define from a [Simulink.Bus](../../simulink/slref/simulink.bus.html) (Simulink) object. You can use Stateflow structures to bundle data of different sizes and types together into a single data object. For more information, see Access Bus Signals.

Index Substructures and Fields

To index substructures and fields of Stateflow structures, use dot notation. The first part of a name identifies the parent structure. Subsequent parts identify the children along a hierarchical path. The children can be individual fields or fields that contain other structures (also called substructures). The names of the fields of a Stateflow structure match the names of the elements of the Simulink.Bus object that defines the structure. When a field contains a vector, matrix, or array, you can access its elements by using the indexing notation supported by the action language of your chart.

For example, the chart in this model contains an input structure (in), an output structure (out), a local structure (localbus), and a local array of structures (subBusArray).

This list illustrates expressions that combine dot notation and numeric indices based on the structure specifications for this example:

Because the chart uses MATLAB® as the action language, you access the elements of the arrays in this example by using one-based indexing delimited by parentheses. In charts that use C as the action language, use zero-based indexing delimited by brackets. For more information, see Operations for Vectors and Matrices in Stateflow.

Assign Values to Structures and Fields

You can write to any Stateflow structure that has a scope other than Input. You can assign values to the entire structure, to a substructure, or to a single field.

For instance, the chart in this example makes these assignments:

Run the Simulation

When you simulate the example, the chart uses the values of the field sb of the input structure to populate the fields a, b, and c of the output structure. The parameter idx selects the element of the array of structures subBusArray to use as the substructure sb of the output. In this example, idx equals 2, so the chart uses the values of the fields a, b, c of the input structure to populate the substructure.

When you use other values for idx, the substructure out.sb contains the same values as in.sb, the transpose of in.sb, or a 3-by-3 magic square.

See Also

Simulink.Bus (Simulink)

Topics