Typedef
Create data type aliases by generating typedef
statements.
C Construct
typedef float float_32;
Procedure
To create a data type alias in Simulink®, use a Simulink.AliasType
. The code generator creates a typedef
statement in the generated code.
The built-in Simulink data type single
corresponds to the C data type float
.
1. At the command prompt, create a Simulink.AliasType
object named float_32
that represents an alias of single
.
float_32 = Simulink.AliasType('single');
2. Open the example model ex_typedef
.
3. In the Model Data Editor, open the Inports/Outports tab.
4. From the Change View menu, select Design
.
5. In the model, select the Inport block.
6. In the Model Data Editor, for the Inport block, set Data Type to float_32
. Click Refresh data type
if float_32
is not available in the drop down options.
7. Open the Embedded Coder app.
8. In the C Code tab, select Code Interface > Individual Element Code Mappings.
9. In the Code Mappings editor, on the Inports tab, set Storage Class for the Inport block to ExportedGlobal
. The Inport block appears in the generated code as a separate global variable.
10. In the Property Inspector, for the Inport block, set the Code > Identifier property to mySig
.
11. To build the model and generate code, press Ctrl+B.
Results
The generated header file ex_typedef.h
defines the data type alias float_32
.
#ifndef DEFINED_TYPEDEF_FOR_float_32_ #define DEFINED_TYPEDEF_FOR_float_32_ typedef real32_T float_32; #endif
By default, the code generator also creates the alias real32_T
, which corresponds to the C data type float
. You can see the typedef
statement in the generated header file rtwtypes.h
.
typedef float real32_T;
The generated source file ex_typedef.c
uses float_32
to define the global variable mySig.
/* Exported block signals */ float_32 mySig; /* '<Root>/In1' */