Main Content

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');

float_32 = Simulink.AliasType('single');

2. Open the example model ex_typedef.

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.

set_param('ex_typedef/In1','OutDataTypeStr','float_32');

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.

cm = coder.mapping.api.get('ex_typedef');
setInport(cm,'In1','StorageClass','ExportedGlobal','Identifier','mySig');

11. To build the model and generate code, press Ctrl+B .

currentDir = pwd;
[~,cgDir] = rtwdemodir();
evalc('slbuild(''ex_typedef'')');

Results

The generated header file ex_typedef.h defines the data type alias float_32.

file = fullfile('ex_typedef_ert_rtw','ex_typedef.h');
rtwdemodbtype(file,'#ifndef DEFINED_TYPEDEF_FOR_float_32_', ...
    '/* External outputs',1,1);
#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.

file = fullfile('ex_typedef_ert_rtw','rtwtypes.h');
rtwdemodbtype(file,'typedef float real32_T;', ...
    'typedef float real32_T;',1,1);
typedef float real32_T;

The generated source file ex_typedef.c uses float_32 to define the global variable mySig.

file = fullfile('ex_typedef_ert_rtw','ex_typedef.c');
rtwdemodbtype(file,'/* Exported block signals */', ...
    '/* External outputs',1,1);
/* Exported block signals */
float_32 mySig;                        /* '<Root>/In1' */

See Also

Related Topics