Index and Assign Values to Stateflow Structures
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
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.Simulink.Bus
(Simulink)
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
).
The chart defines the input structure
in
, the output structureout
, and the local structurelocalbus
by using theSimulink.Bus
objectBusObject
. These structures have four fields:sb
,a
,b
, andc
.The field
sb
is a substructure defined from theSimulink.Bus
objectSubBus
. This substructure has one field calledele
.The chart defines the local array of structures
subBusArray
by using theSimulink.Bus
objectSubBus
. The array has size 4. Each element in the array is a structure with one field calledele
.
This list illustrates expressions that combine dot notation and numeric indices based on the structure specifications for this example:
in.c
— Fieldc
of the input structurein
in.a(1)
— First element of the vector fielda
of the input structurein
out.sb
— Substructuresb
of the output structureout
out.sb.ele
— Fieldele
of the substructureout.sb
out.sb.ele(2,2)
— Element in the second row, second column of the fieldele
of the substructureout.sb
subBusArray(1)
— First element of the array of structuressubBusArray
subBusArray(1).ele
— Fieldele
of the structuresubBusArray(1)
subBusArray(1).ele(3,4)
— Element in the third row, fourth column of the fieldele
of structuresubBusArray(1)
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.
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 the other way around), 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.
For instance, the chart in this example makes these assignments:
localbus = sb2abc(in.sb)
— The structurelocalbus
and the output argument of the MATLAB® functionsb2abc
are defined from the sameSimulink.Bus
objectBusObject
. The function decomposes its input into three components: a vector, a 3-by-2 matrix, and a scalar. The function returns these components as the fieldsa
,b
, andc
of its output. For more information on this function, see Access Simulink Bus Signals in MATLAB Functions.subBusArray(1) = in.sb
— The structuresubBusArray(1)
and the substructurein.sb
are defined from the sameSimulink.Bus
objectSubBus
.subBusArray(2) = abc2sb(in)
— The structuresubBusArray(2)
and the output argument of the graphical functionabc2sb
are defined from the sameSimulink.Bus
objectSubBus
. The function combines the values of the fieldsa
,b
, andc
from its input and rearranges them in a 3-by-3 matrix of typeint8
. It returns this matrix as the fieldele
of its output.subBusArray(3).ele = transpose(in.sb.ele)
— The fieldsubBusArray(3).ele
has the same type and size as the result oftranspose(in.sb.ele)
. Both are 3-by-3 matrices of typeint8
.subBusArray(4).ele = int8(magic(3))
— The fieldsubBusArray(4).ele
has the same type and size as the result ofint8(magic(3))
. Both are 3-by-3 matrices of typeint8
.out = localbus
— Bothout
andlocalbus
are defined from the sameSimulink.Bus
objectBusObject
.out.sb = subBusArray(idx)
— The substructureout.sb
and the structuresubBusArray(idx)
are defined from the sameSimulink.Bus
objectSubBus
.
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)