You can create nested structures of signal data in the generated code.
typedef struct { double signal1; double signal2; double signal3; } B_struct_type; typedef struct { double signal1; double signal2; } C_struct_type; typedef struct { B_struct_type subStruct_B; C_struct_type subStruct_C; } A_struct_type;
To represent a structure type in a model, create a Simulink.Bus
object. Use the object as the data type of bus signals in your model.
To nest a structure inside another structure, use a bus object as the data type of a signal element in another bus object.
1. Create the ex_signal_nested_struct
model with Gain blocks, Bus Creator blocks, and a Unit Delay block. The Gain and Unit Delay blocks make the structure more identifiable in the generated code.
2. To configure a Bus Creator block to accept three inputs, in the block dialog box, set Number of inputs to 3
.
3. In the toolstrip, on the Modeling tab, under Design, click Bus Editor.
Use the Bus Editor to create a Simulink.Bus
object named A_struct_type
that contains two signal elements: subStruct_B
and subStruct_C
. To create bus objects with the Bus Editor, see Create and Specify Simulink.Bus Objects (Simulink). This bus object represents the top-level structure type that you want the generated code to use.
4. For the subStruct_B
element, set DataType to Bus: B_struct_type
. Use a similar type name for subStruct_C
.
Each signal element in A_struct_type
uses another bus object as a data type. Now, these elements represent substructures.
5. Use the Bus Editor to create the Simulink.Bus
objects B_struct_type
(with three signal elements) and C_struct_type
(with two signal elements).
6. In the dialog box of the Bus Creator block that collects the three Gain signals, set Output data type to Bus: B_struct_type
. Click Apply.
7. Select Output as nonvirtual bus and click OK.
8. In the dialog box of the other subordinate Bus Creator block, set Output data type to Bus: C_struct_type
and select Output as nonvirtual bus. Click OK.
9. In the last Bus Creator block dialog box, set Output data type to Bus: A_struct_type
and select Output as nonvirtual bus. Click OK.
10. On the Modeling tab, click Model Data Editor.
11. In the Model Data Editor, on the Signals tab, from the Change view drop-down list, select Code
.
12. In the model, click the output signal of the A_struct_type
Bus Creator block, which feeds the Unit Delay block.
13. In the Model Data Editor, for the output of the Bus Creator block, set Name to sig_struct_var
.
14. Set Storage Class to ExportedGlobal
. With this setting, the output of the Bus Creator block appears in the generated code as a separate global structure variable named sig_struct_var
.
15. Generate code from the model.
The generated header file ex_signal_nested_struct_types.h
defines the structure types. Each structure type corresponds to a Simulink.Bus
object.
typedef struct { real_T signal1; real_T signal2; real_T signal3; } B_struct_type; typedef struct { real_T signal1; real_T signal2; } C_struct_type; typedef struct { B_struct_type subStruct_B; C_struct_type subStruct_C; } A_struct_type;
The generated source file ex_signal_nested_struct.c
allocates memory for the global structure variable sig_struct_var
. By default, the name of the A_struct_type
Bus Creator block is Bus Creator2
.
/* Exported block signals */ A_struct_type sig_struct_var; /* '<Root>/Bus Creator2' */
In the same file, in the model step
function, the algorithm accesses sig_struct_var
and the fields of sig_struct_var
.