Structure Operations

Index Sub-Structures and Fields

You index substructures and fields of Stateflow® structures by using dot notation. With dot notation, the first text string identifies the parent object, and subsequent text strings 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). By default, the names of the fields of a Stateflow structure match the names of the elements of the Simulink.Bus object that defines the structure.

Suppose that you have the following modelmodel:

In this example, the SubBus and BusObject blocks to the left of the chart are Bus Creator blocks. The BusObject block to the right of the chart is a Bus Selector block.

The following structures are defined in the chart:

Name of StructureScopeDefined By Simulink.Bus Object
inInputBusObject
outOutputBusObject
subbusLocalSubBus

The Simulink.Bus objects that define these structures have the following elements:

By default, Stateflow structures in and out have the same fields — sb, a, b, and c — as the elements of Simulink.Bus object BusObject. Similarly, the Stateflow structure subbus has the same field ele as the element of Simulink.Bus object SubBus. Based on these specifications, the following table shows how the Stateflow chart resolves symbols in dot notation for indexing fields of the structures in this example:

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

Guidelines for Assignment of Values

You can assign values to any Stateflow structure except input structures — that is, a structure with scope equal to Input. Here are the guidelines for assigning values to output, local, parameter, and temporary structures:

OperationConditions

Assign one structure to another structure

You must define both structures with the same Simulink.Bus object in the base workspace.

Assign one structure to a substructure of a different structure and vice versa

You must define the structure with the same Simulink.Bus object in the base workspace as the substructure.

Assign a field of one structure to a field of another structure

The fields must have the same type and size.

    Note:   In this case, you do not need to define the Stateflow structures with the same Simulink.Bus object in the base workspace.

For example, the following table presents valid and invalid structure assignments based on specifications for the sfbus_demosfbus_demo model, as described in Connect Structures in Charts to External Bus Signals:

AssignmentValid or Invalid?Rationale

outbus = inbus;

Valid

Both outbus and inbus are defined by the same Simulink.Bus object, COUNTERBUS.

inbus = outbus;

Invalid

You cannot write to input structures.

inbus.limits = outbus.limits;

Invalid

You cannot write to fields of input structures.

counterbus_struct = inbus;

Valid

Both counterbus_struct and inbus are defined by the same Simulink.Bus object, COUNTERBUS.

counterbus_struct.inputsignal = inbus.inputsignal;

Valid

Both counterbus_struct.inputsignal and inbus.inputsignal have the same type and size because they each reference field inputsignal, a substructure of the Simulink.Bus object COUNTERBUS.

outbus.limits.upper_saturation_limit = inbus.inputsignal.input;

Valid

The field upper_saturation_limit from limits, a substructure of COUNTERBUS, has the same type and size as the field input from inputsignal, a different substructure of COUNTERBUS.

outbus.limits = inbus.inputsignal;

Invalid

The substructure limits is defined by a different Simulink.Bus object than the substructure inputsignal.

Get Addresses

When you write custom functions that take structure pointers as arguments, you must pass the structures by address. To get addresses of Stateflow structures and structure fields, use the & operator, as in the following examples:

  • &in — Address of Stateflow structure in

  • &in.b — Address of field b in Stateflow structure in

The model sfbus_demosfbus_demo contains a custom C function counterbusFcn that takes structure pointers as arguments, defined as follows in a custom header file:

...
extern void counterbusFcn
            (COUNTERBUS *u1, int u2, COUNTERBUS *y1, int *y2);
...

To call this function, you must pass addresses to two structures defined by the Simulink.Bus object COUNTERBUS, as in this example:

counterbusFcn(&counterbus_struct, u2, &outbus, &y2);

See Connect Structures in Charts to External Bus Signals for a description of the structures defined in sfbus_demosfbus_demo.

Was this topic helpful?