| Contents | Index |
| On this page… |
|---|
To generate a typedef definition, use a Simulink.AliasType data object.
typedef double float_64;
Create the ex_get_typedef model with a Gain block.

In the Gain block parameter dialog box, select the Parameter Attributes tab, and specify the Parameter data type as double.
Right-click the u1 signal and select Signal Properties. In the Signal Properties dialog box, select Signal name must resolve to Simulink signal object.
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.
Create a new alias type by using a Simulink.AliasType data object. At the MATLAB command line, enter:
float_64 = Simulink.AliasType;
In the base workspace, double-click float_64. The Simulink.AliasType dialog box opens.
Specify the Base type parameter as double. Click Apply and OK.
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).
Click Apply and OK.
Press Ctrl+B to generate code.
Note An alternative method for defining a typedef is to import the alias type from a custom header file. If you want to import all the typedefs from a C header file, using this alternative method is useful. |
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;
To generate a structure containing parameters, use a mpt.Parameter object with a Struct (custom) storage class.
typdef struct {
double p1;
double p2;
double p3;
} my_struct_type;
my_struct_type my_struct={1.0,2.0,3.0};Create the ex_struct_param model with three Constant blocks and three Outport blocks.

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;
In the base workspace, double-click one of the parameter data objects to open the mpt.Parameter dialog box.
Specify a Value parameter for each parameter object.
Specify the Storage class parameter as Struct (Custom) for each parameter object.
In the Custom Attributes section, specify the Struct name as my_struct. Click Apply and OK.
Press Ctrl+E to open the Configuration Parameters dialog box.
Open the Optimization > Signals and Parameters pane, and select the Inline parameters parameter.
Click Apply and OK.
Press Ctrl+B to generate code.
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
};
To generate a structure containing parameters, use a mpt.Signal object with a Struct (custom) storage class or a Simulink non-virtual bus object.
typedef struct {
double u1;
double u2;
double u3;
} my_signals; Structure for Signals Using a 'Struct' Custom Storage Class
Structure for Signals Using a Simulink Non-Virtual Bus Object
Create the ex_signal_struct_csc model using the blocks shown and follow the steps to configure the signals and model.

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.
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.
At the MATLAB command line, create a mpt.Signal data object for each input signal.
u1 = mpt.Signal; u2 = mpt.Signal; u3 = mpt.Signal;
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.
Specify the Data type parameter as auto and the Storage class parameter as Struct (custom).
Click Apply and OK.
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;
Create the ex_signal_struct_bus model using the blocks shown and follow the steps to configure the bus object and model.

Add the Inport blocks, an Outport block, and a Bus Creator block to your diagram.
Double-click the Bus Creator block to open the block parameter dialog box.
Specify the Number of inputs parameter as 3. Click Apply.
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.
Label the signals as shown in the model diagram.
In the Bus Creator block parameter dialog box, Signals in bus now displays the signals connected to the Bus Creator block.
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.
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.
Click Apply and OK.
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;
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.
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;Create the ex_nested_structure model using the blocks shown and follow the steps to configure the bus objects and model.

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.
Drag a Bus Creator block into your model. Configure the Bus Creator block so that it takes in signals from different buses.
Double-click the Bus Creator block to open the block parameter dialog box.
Specify the Number of inputs parameter as 2. Click Apply.
In your model diagram, connect the two bus outports to the inports of the new Bus Creator block.
Label the signals as shown in the model diagram.
In the Bus Creator block parameter dialog box, Signals in bus now displays the signals, y1 and y2, connected to the Bus Creator block.
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.
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.
Click Apply and OK.
Press Ctrl+B to generate code.
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
One way to create bitfields in the generated code is by using a mpt.Parameter object with Bitfield (Custom) storage class.
typedef struct {
unsigned int p1 : 1;
unsigned int p2 : 2;
unsigned int p3 : 3;
} my_struct_typeUsing the model, ex_struct_param, in Structures for Parameters, rename the model as ex_struct_bitfield_CSC.
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;
In the base workspace, double-click one of the parameter data objects to open the mpt.Parameter dialog box.
Specify the Value parameter for each parameter object.
Specify the Storage class parameter as Bitfield (Custom) for each parameter object.
In the Custom Attributes section, specify the Struct name as my_struct. Click Apply and OK.
Specify the data objects for each parameter.

Press Ctrl+E to open the Configuration Parameters dialog box.
Open the Optimization > Signals and Parameters pane, and select the Inline parameters parameter.
Click Apply and OK.
Press Ctrl+B to generate code.
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;
![]() | Preprocessor Directives | Arrays | ![]() |

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 |