主要内容

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 aSimulink.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.Busobject 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 structurein, the output structure出去, 和the local structurelocalbusby using theSimulink.BusobjectBusObject。These structures have four fields:sb,a,b, 和c

  • sb是从Simulink.Busobjectsubbu。This substructure has one field calledele

  • The chart defines the local array of structuresSubbusarrayby using theSimulink.Busobjectsubbu。数组具有尺寸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- 场地eleof the structureSubbusarray(1)

  • Subbusarray(1).Ele(3,4)— Element in the third row, fourth column of the fieldeleof 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 sameSimulink.Bus基本工作区中的对象。

  • To assign one structure to a substructure of a different structure (or the other way around), define the structure and substructure from the sameSimulink.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 differentSimulink.Busobjects.

例如,此示例中的图表进行了这些作业:

  • localbus = sb2abc(in.sb)— The structurelocalbus和MATLAB®函数的输出参数sb2abcare defined from the sameSimulink.BusobjectBusObject。该函数将其输入分解为三个组件:矢量,一个3 by-2矩阵和标量。该函数将这些组件作为字段返回a,b, 和cof its output. For more information on this function, seeAccess Simulink Bus Signals in MATLAB Functions

  • Subbusarray(1) = in.sb— The structureSubbusarray(1)和the substructurein.sbare defined from the sameSimulink.Busobjectsubbu

  • subbusarray(2)= ABC2SB(in)— The structureSubbusarray(2)和the output argument of the graphical functionABC2SBare defined from the sameSimulink.Busobjectsubbu。该函数结合了字段的值a,b, 和c从输入中,将它们重新排列3 x-3矩阵int8。It returns this matrix as the fieldeleof its output.

  • subbusarray(3).ele = transpose(in.sb.ele)- 场Subbusarray(3).elehas 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).elehas the same type and size as the result ofint8(魔术(3))。Both are 3-by-3 matrices of typeint8

  • 出去= localbus- 两个都出去localbusare defined from the sameSimulink.BusobjectBusObject

  • 出去。sb = subBusArray(idx)- 子结构出去。sb和结构Subbusarray(idx)are defined from the sameSimulink.Busobjectsubbu

Run the Simulation

当您模拟示例时,图表使用字段的值sb输入结构to populate the fieldsa,b, 和c输出结构。The parameteridxselects the element of the array of structuresSubbusarrayto use as the substructuresbof the output. In this example,idx等于2,因此图表使用字段的值a,b,c输入结构以填充子结构。

当您使用其他值idx,子结构出去。sbcontains the same values asin.sb, the transpose ofin.sb,或3 x 3魔法广场。

See Also

(Simulink)

相关话题