Index and Assign Values to Stateflow Structures

Stateflow® structures enable you to bundle data of different size and type together into a single Simulink.Bus object. Using dot notation and numeric indices, you can access and modify the contents of a Stateflow structure. For more information, see Access Bus Signals Through Stateflow Structures.

Index Substructures and Fields

To index substructures and fields of Stateflow structures, use dot notation. The first part of a name identifies the parent object. Subsequent parts identify the children along a hierarchical path. When the parent is a structure, its children are 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.

For example, the C chart in this model contains an input structure in, an output structure out, and a local structure subbus.

StructureScopeSimulink.Bus Object
inInputBusObject
outOutputBusObject
subbusLocalSubBus

The fields of the input structure in and the output structure out have the same name as the elements of Simulink.Bus object BusObject that defines them: sb, a, b, and c. The field of the local structure subbus has the same name ele as the element of Simulink.Bus object SubBus. This table lists how the Stateflow chart resolves symbols in dot notation for indexing the fields of these structures.

Dot NotationSymbol Resolution
in.cField c of input structure in
out.sbSubstructure sb of output structure out
in.a[0]First value of the vector field a of input structure in
subbus.ele[1][1]Value in the second row, second column of field ele of local structure subbus
in.sb.ele[2][3]Value in the third row, fourth column of field ele of substructure in.sb

Note

In this example, the Stateflow chart uses brackets and zero-based indexing for vectors and arrays because C is the action language for the chart. For more information, see Differences Between MATLAB and C as Action Language Syntax.

Assign Values to Structures and Fields

You can assign values to any Stateflow structure, substructure, or a field of a structure with a scope different from Input.

  • To assign one structure to another structure, define both structures from the same Simulink.Bus object in the base workspace.

  • To assign one structure to a substructure of a different structure (or vice versa), define the structure and substructure from the same Simulink.Bus object.

  • To assign a field of one structure to a field of another structure, the fields must have the same type and size. You can define the Stateflow structures from different Simulink.Bus objects.

This table presents valid and invalid structure assignments based on the structure specifications for the previous example.

AssignmentValid or Invalid?Explanation

in = out;

Invalid

You cannot write to input structures.

out = in;

Valid

Both in and out are defined from the same Simulink.Bus object BusObject.

subbus = in;

Invalid

The structures subbus and in are defined from different Simulink.Bus objects.

in.sb = subbus;

Invalid

You cannot write to substructures of input structures.

out.sb = subbus;

Valid

The substructure out.sb and the structure subbus are defined from the same Simulink.Bus object SubBus.

in.c = out.c;

Invalid

You cannot write to fields of input structures.

out.sb.ele = subbus.ele;

Valid

The field out.sb.ele has the same type and size as the field subbus.ele (3-by-3 matrices).

subbus.ele[1][1] = in.c;

Valid

The field subbus.ele[1][1] has the same type and size as the field in.c (scalars).

See Also

Related Topics