Structures of Signals

This example shows how to create a structure of signal data in the generated code.

C Construct

typedef struct {
   double signal1;
   double signal2;
   double signal3;
} my_signals_type;

Procedure

1. 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.

2. Create the ex_signal_struct model by using Gain blocks, a Bus Creator block, and a Unit Delay block. The Gain and Unit Delay blocks make the structure more identifiable in the generated code.

3. To configure the Bus Creator block to accept three inputs, in the block dialog box, set Number of inputs to 3.

4. In the toolstrip, on the Modeling tab, under Design, click Bus Editor.

5. Use the Bus Editor to create a Simulink.Bus object named my_signals_type that contains three signal elements: signal1, signal2, and signal3. See Create and Specify Simulink.Bus Objects (Simulink).

This bus object represents the structure type that you want the generated code to use.

6. In the Bus Creator block dialog box, set Output data type to Bus: my_signals_type.

7. Select Output as nonvirtual bus. Click OK. A nonvirtual bus appears in the generated code as a structure.

8. On the Modeling tab, click Model Data Editor. In the Model Data Editor, on the Signals tab, from the Change view drop-down list, select Code.

9. In the model, click the output signal of the Bus Creator block.

10. In the Model Data Editor, for the output of the Bus Creator block, set Name to sig_struct_var.

11. Set Storage Class to ExportedGlobal. The output of the Bus Creator block appears in the generated code as a separate global structure variable named sig_struct_var.

12. Generate code from the model.

Results

The generated header file ex_signal_struct_types.h defines the structure type my_signals_type.

typedef struct {
  real_T signal1;
  real_T signal2;
  real_T signal3;
} my_signals_type;

The source file ex_signal_struct.c allocates memory for the global variable sig_struct_var, which represents the output of the Bus Creator block.

/* Exported block signals */
my_signals_type sig_struct_var;        /* '<Root>/Bus Creator' */

In the same file, in the model step function, the algorithm accesses sig_struct_var and the fields of sig_struct_var.

See Also

Related Topics