Skip to Main Content Skip to Search
Product Documentation

Structures

Typedef

To generate a typedef definition, use a Simulink.AliasType data object.

C Construct

typedef double float_64;

Procedure

  1. Create the ex_get_typedef model with a Gain block.

  2. In the Gain block parameter dialog box, select the Parameter Attributes tab, and specify the Parameter data type as double.

  3. Right-click the u1 signal and select Signal Properties. In the Signal Properties dialog box, select Signal name must resolve to Simulink signal object.

  4. Right-click the y1 signal and select Signal Properties. In the Signal Properties dialog box, select the Code Generation tab, and specify the Storage class parameter as ExportedGlobal.

  5. Create a new alias type by using a Simulink.AliasType data object. At the MATLAB command line, enter:

    float_64 = Simulink.AliasType;
  6. In the base workspace, double-click float_64. The Simulink.AliasType dialog box opens.

  7. Specify the Base type parameter as double. Click Apply and OK.

  8. Create a data object for the u1 signal. In the base workspace, select Add > Simulink Signal, and name it u1. Specify the Data type parameter as float_64 and the Storage class parameter as Global(custom).

      Note   You can also specify an output data type for Simulink blocks using the new alias type.

  9. Click Apply and OK.

  10. Press Ctrl+B to generate code.

Results

The generated code includes the typedef definition, which is declared within #ifndef and #endif statements in the ex_get_typedef_types.h file.

#ifndef _DEFINED_TYPEDEF_FOR_float_64_
#define _DEFINED_TYPEDEF_FOR_float_64_

typedef real_T float_64;
typedef creal_T cfloat_64;

#endif

The generated code also includes the declaration of the Simulink data objects of the alias type in ex_get_typedef.c.

float_64 y1;
float_64 u1;

Structures for Parameters

To generate a structure containing parameters, use a mpt.Parameter object with a Struct (custom) storage class.

C Construct

typdef struct {
		double p1;
		double p2;
		double p3;

} my_struct_type;

my_struct_type my_struct={1.0,2.0,3.0};

Procedure

  1. Create the ex_struct_param model with three Constant blocks and three Outport blocks.

  2. Create a data object for each parameter, p1, p2, and p3. At the MATLAB command line, enter:

    p1 = mpt.Parameter;
    p2 = mpt.Parameter;
    p3 = mpt.Parameter;
  3. In the base workspace, double-click one of the parameter data objects to open the mpt.Parameter dialog box.

  4. Specify a Value parameter for each parameter object.

  5. Specify the Storage class parameter as Struct (Custom) for each parameter object.

  6. In the Custom Attributes section, specify the Struct name as my_struct. Click Apply and OK.

  7. Press Ctrl+E to open the Configuration Parameters dialog box.

  8. Open the Optimization > Signals and Parameters pane, and select the Inline parameters parameter.

  9. Click Apply and OK.

  10. Press Ctrl+B to generate code.

Results

The generated code includes the typedef definition for a structure, which is declared in the ex_struct_param_types.h file.

/* Type definition for custom storage class: Struct */
   typedef struct my_struct_tag {
      real_T p1;
      real_T p2;
      real_T p3;
   } my_struct_type;

The generated code also includes the declaration of my_struct in ex_struct_param.c.

/* Definition for custom storage class: Struct */
my_struct_type my_struct = {
     /* p1 */
     1.0,

     /* p2 */
     2.0,

     /* p3 */
     3.0
};

Structures for Signals

To generate a structure containing parameters, use a mpt.Signal object with a Struct (custom) storage class or a Simulink non-virtual bus object.

C Construct

typedef struct {
   double u1;
   double u2;
   double u3;
} my_signals;   

Modeling Patterns

Structure for Signals Using a 'Struct' Custom Storage Class

Structure for Signals Using a Simulink Non-Virtual Bus Object

Structure for Signals Using a 'Struct' Custom Storage Class

Procedure.  

  1. Create the ex_signal_struct_csc model using the blocks shown and follow the steps to configure the signals and model.

  2. Double-click a Gain block to open the block parameter dialog box. Set the values of the Gain blocks as shown in the model diagram.

  3. Right-click the u1 signal and select Signal Properties. In the Signal Properties dialog box, select Signal name must resolve to Simulink signal object. Repeat for signals u2 and u3.

  4. At the MATLAB command line, create a mpt.Signal data object for each input signal.

    u1 = mpt.Signal;
    u2 = mpt.Signal;
    u3 = mpt.Signal;

      Note   You can also create a data object in the Model Explorer base workspace, by selecting Add > MPT Signal.

  5. In the base workspace, configure each of the data objects, u1, u2, and u3. Double-click a data object, to open the mpt.Signal parameter dialog box.

  6. Specify the Data type parameter as auto and the Storage class parameter as Struct (custom).

  7. Click Apply and OK.

  8. Press Ctrl+B to generate code.

Results.  The generated code includes the typedef definition for a structure, which is declared in the ex_signal_struct_csc_types.h file.

/* Type definition for custom storage class: Struct */
typedef struct my_signal_struct_tag {
   real_T u1;
   real_T u2;
   real_T u3;
 } my_signal_struct_type;

The generated code also includes the declaration of my_signal_struct in ex_signal_struct_csc.c.

/* Definition for custom storage class: Struct */
my_signal_struct_type my_signals;

Structure for Signals Using a Simulink Non-Virtual Bus Object

Procedure.  

  1. Create the ex_signal_struct_bus model using the blocks shown and follow the steps to configure the bus object and model.

  2. Add the Inport blocks, an Outport block, and a Bus Creator block to your diagram.

  3. Double-click the Bus Creator block to open the block parameter dialog box.

  4. Specify the Number of inputs parameter as 3. Click Apply.

  5. In your model diagram, connect the three Inport blocks to the three inports of the Bus Creator block. Also, connect the outport of the Bus Creator block to the Outport block.

  6. Label the signals as shown in the model diagram.

  7. In the Bus Creator block parameter dialog box, Signals in bus now displays the signals connected to the Bus Creator block.

  8. Create a bus object named MySignals that includes signals u1,u2, and u3. For more information on creating bus objects, see Using the Bus Editor. Once the bus object, MySignals, is created, it appears in the base workspace.

  9. In the Bus Creator block parameter dialog box, select the Output as nonvirtual bus parameter, which specifies that bus signals must be grouped into a structure in the generated code.

  10. Click Apply and OK.

  11. Press Ctrl+B to generate code.

Results.  The generated code includes the typedef definition for a structure, which is declared in the signal_struct_bus_types.h file.

typedef struct {
   real_T u1;
   real_T u2;
   real_T u3;
} MySignals;

Nested Structures

One way to create nested structures of signals in the generated code is by using multiple non-virtual bus objects. When nesting bus objects, all of the bus objects must either be non-virtual, or all of them must be virtual.

C Construct

typedef struct {
   double u1;
   double u2;
   double u3;
} my_signals123;

typedef struct {
   double u4;
   double u5;
   double u6;
} my_signals456;

typedef struct {
   my_signals123 y1;
   my_signals456 y2;
 } nested_signals;

Procedure

  1. Create the ex_nested_structure model using the blocks shown and follow the steps to configure the bus objects and model.

  2. For each bus in the model, follow the instructions for Structure for Signals Using a Simulink Non-Virtual Bus Object, creating bus objects My_Signals_123 and My_Signals_456.

  3. Drag a Bus Creator block into your model. Configure the Bus Creator block so that it takes in signals from different buses.

  4. Double-click the Bus Creator block to open the block parameter dialog box.

  5. Specify the Number of inputs parameter as 2. Click Apply.

  6. In your model diagram, connect the two bus outports to the inports of the new Bus Creator block.

  7. Label the signals as shown in the model diagram.

  8. In the Bus Creator block parameter dialog box, Signals in bus now displays the signals, y1 and y2, connected to the Bus Creator block.

  9. Create a bus object named Nested_Signals that includes signals y1 and y2, where the DataType for y1 is My_Signals_123 and the DataType for y2 is My_Signals_456.

    For more information on creating bus objects, see Using the Bus Editor. Once the bus object, Nested_Signals, is created, it appears in the base workspace.

  10. In the Bus Creator block parameter dialog box, select the Output as nonvirtual bus parameter, which specifies that bus signals must be grouped into a structure in the generated code.

  11. Click Apply and OK.

  12. Press Ctrl+B to generate code.

Results

The generated code includes the typedef definitions for structures, which are declared in the ex_nested_structure_types.h file.

#ifndef _DEFINED_TYPEDEF_FOR_My_Signals_123_
#define _DEFINED_TYPEDEF_FOR_My_Signals_123_
   
typedef struct {
   real_T u1;
   real_T u2;
   real_T u3;
} My_Signals_123;
   
#endif
   
#ifndef _DEFINED_TYPEDEF_FOR_My_Signals_456_
#define _DEFINED_TYPEDEF_FOR_My_Signals_456_
   
typedef struct {
   real_T u4;
   real_T u5;
   real_T u6;
} My_Signals_456;
   
#endif
   
#ifndef _DEFINED_TYPEDEF_FOR_Nested_Signals_
#define _DEFINED_TYPEDEF_FOR_Nested_Signals_
   
typedef struct {
   My_Signals_123 y1;
   My_Signals_456 y2;
} Nested_Signals;
   
#endif

Bitfields

One way to create bitfields in the generated code is by using a mpt.Parameter object with Bitfield (Custom) storage class.

C Construct

typedef struct {
   unsigned int p1 : 1;
   unsigned int p2 : 2;
   unsigned int p3 : 3;
} my_struct_type

Procedure

  1. Using the model, ex_struct_param, in Structures for Parameters, rename the model as ex_struct_bitfield_CSC.

  2. Create a data object for each parameter, p1, p2, and p3. At the MATLAB command line, enter:

    p1 = mpt.Parameter;
    p2 = mpt.Parameter;
    p3 = mpt.Parameter;
  3. In the base workspace, double-click one of the parameter data objects to open the mpt.Parameter dialog box.

  4. Specify the Value parameter for each parameter object.

  5. Specify the Storage class parameter as Bitfield (Custom) for each parameter object.

  6. In the Custom Attributes section, specify the Struct name as my_struct. Click Apply and OK.

  7. Specify the data objects for each parameter.

  8. Press Ctrl+E to open the Configuration Parameters dialog box.

  9. Open the Optimization > Signals and Parameters pane, and select the Inline parameters parameter.

  10. Click Apply and OK.

  11. Press Ctrl+B to generate code.

Results

The generated code of the model, ex_struct_bitfield_CSC, includes the typedef definition for a Bitfield, which is declared in the ex_struct_bitfield_CSC_types.h file.

 /* Type definition for custom storage class: BitField */
 typedef struct my_struct_tag {
   uint_T p1 : 1;
   uint_T p2 : 1;
   uint_T p3 : 1;
 } my_struct_type;
  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS