Index and Assign Values to Stateflow Structures
这个例子展示了如何访问和修改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)
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 theSimulink.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 (出去
), a local structure (localbus
), and a local array of structures (Subbusarray
).
The chart defines the input structure
in
, the output structure出去
, 和the local structurelocalbus
by using theSimulink.Bus
objectBusObject
。These structures have four fields:sb
,a
,b
, 和c
。场
sb
是从Simulink.Bus
objectsubbu
。This substructure has one field calledele
。The chart defines the local array of structures
Subbusarray
by using theSimulink.Bus
objectsubbu
。数组具有尺寸4。数组中的每个元素都是一个结构,一个字段称为ele
。
This list illustrates expressions that combine dot notation and numeric indices based on the structure specifications for this example:
in.c
- 场地c
输入结构in
in.a(1)
— First element of the vector fielda
输入结构in
出去。sb
— Substructuresb
输出结构出去
出去。sb.ele
- 场地ele
子结构出去。sb
出去。sb.ele(2,2)
— Element in the second row, second column of the fieldele
子结构出去。sb
Subbusarray(1)
- 结构阵列的第一个要素Subbusarray
Subbusarray(1).ele
- 场地ele
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, seeSupported Operations for Vectors and Matrices。
为结构和字段分配值
You can write to any Stateflow structure that has a scope other than输入
。您可以为整个结构,子结构或单个字段分配值。
To assign one structure to another structure, define both structures from the same
Simulink.Bus
基本工作区中的对象。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
目的。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.
例如,此示例中的图表进行了这些作业:
localbus = sb2abc(in.sb)
— The structurelocalbus
和MATLAB®函数的输出参数sb2abc
are defined from the sameSimulink.Bus
objectBusObject
。该函数将其输入分解为三个组件:矢量,一个3 by-2矩阵和标量。该函数将这些组件作为字段返回a
,b
, 和c
of its output. For more information on this function, seeAccess Simulink Bus Signals in MATLAB Functions。Subbusarray(1) = in.sb
— The structureSubbusarray(1)
和the substructurein.sb
are defined from the sameSimulink.Bus
objectsubbu
。subbusarray(2)= ABC2SB(in)
— The structureSubbusarray(2)
和the output argument of the graphical functionABC2SB
are defined from the sameSimulink.Bus
objectsubbu
。该函数结合了字段的值a
,b
, 和c
从输入中,将它们重新排列3 x-3矩阵int8
。It returns this matrix as the fieldele
of its output.subbusarray(3).ele = transpose(in.sb.ele)
- 场Subbusarray(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))
- 场Subbusarray(4).ele
has the same type and size as the result ofint8(魔术(3))
。Both are 3-by-3 matrices of typeint8
。出去= localbus
- 两个都出去
和localbus
are defined from the sameSimulink.Bus
objectBusObject
。出去。sb = subBusArray(idx)
- 子结构出去。sb
和结构Subbusarray(idx)
are defined from the sameSimulink.Bus
objectsubbu
。
Run the Simulation
当您模拟示例时,图表使用字段的值sb
输入结构to populate the fieldsa
,b
, 和c
输出结构。The parameteridx
selects the element of the array of structuresSubbusarray
to use as the substructuresb
of the output. In this example,idx
等于2,因此图表使用字段的值a
,b
,c
输入结构以填充子结构。
当您使用其他值idx
,子结构出去。sb
contains the same values asin.sb
, the transpose ofin.sb
,或3 x 3魔法广场。
See Also
Simulink.Bus
(Simulink)