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.
In the Bus Editor, click File > Add Bus to create a Simulink.Bus
object and name it A_struct_type
. Click File > Add/Insert BusElement to add two signal elements: subStruct_B
and subStruct_C
. For more information, see Create and Specify Simulink.Bus Objects. This bus object represents the top-level structure type that you want the generated code to use.
4. Similarly, create Simulink.Bus
objects B_struct_type
(with three signal elements) and C_struct_type
(with two signal elements).
5. In A_struct_type
object, for the subStruct_B
element, set DataType to Bus: B_struct_type
and subStruct_C
to Bus: C_struct_type
.
Each signal element in A_struct_type
uses another bus object as a data type. Now, these elements represent substructures.
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. Open the Simulink Coder app. In the C Code tab, select Code Interface > Individual Element Code Mappings.
11. Open the Signals/States tab. In the model, select the output signal of the A_struct_type
Bus Creator block, which feeds the Unit Delay block. Click the Add selected signals to code mappings button in the Code Mappings editor.
12. For the added signal, set Storage Class to ExportedGlobal
.
13. In the Property Inspector, set the Code > Identifier property to sig_struct_var
. The output of the Bus Creator block appears in the generated code as a separate global structure variable named sig_struct_var
.
14. 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
.