MATLAB Examples

Create Data Type Alias in the Generated Code

This example shows how to configure the generated code to use a data type name (typedef) that you specify.

Contents

Export Type Definition

When you integrate code generated from a model with code from other sources, your model code can create an exported typedef statement. Therefore, all of the integrated code can use the type. This example shows how to export the definition of a data type to a generated header file.

Create a Simulink.AliasType object named mySingleAlias that acts as an alias for the built-in data type single.

mySingleAlias = Simulink.AliasType('single')
mySingleAlias = 

  AliasType with properties:

    Description: ''
      DataScope: 'Auto'
     HeaderFile: ''
       BaseType: 'single'

Configure the object to export its definition to a header file called myHdrFile.h.

mySingleAlias.DataScope = 'Exported';
mySingleAlias.HeaderFile = 'myHdrFile.h';

Open the model rtwdemo_basicsc.

open_system('rtwdemo_basicsc')

Configure the model to show the generated names of blocks.

set_param('rtwdemo_basicsc','HideAutomaticNames','off')

In the model, select View > Model Data.

In the model, select the Inport block labeled In1.

Use the Data Type column to set the data type to mySingleAlias.

set_param('rtwdemo_basicsc/In1','OutDataTypeStr','mySingleAlias')

In the model, set Configuration Parameters > Code Generation > System target file to ert.tlc. The code generator honors data type aliases such as mySingleAlias only if you select an ERT-based system target file.

set_param('rtwdemo_basicsc','SystemTargetFile','ert.tlc')

Generate code from the model.

rtwbuild('rtwdemo_basicsc')
### Starting build procedure for model: rtwdemo_basicsc
### Successful completion of build procedure for model: rtwdemo_basicsc

In the code generation report, view the file rtwdemo_basicsc_types.h. The code creates a #include directive for the generated file myHdrFile.h.

file = fullfile('rtwdemo_basicsc_ert_rtw','rtwdemo_basicsc_types.h');
rtwdemodbtype(file,'#include "myHdrFile.h"',...
    '#include "myHdrFile.h"',1,1)
#include "myHdrFile.h"

View the file myHdrFile.h. The code uses the identifier mySingleAlias as an alias for the data type real32_T. By default, generated code represents the Simulink data type single by using the identifier real32_T.

The code also provides a macro guard of the form RTW_HEADER_filename_h_. When you export a data type definition to integrate generated code with code from other sources, you can use macro guards of this form to prevent unintentional identifier clashes.

file = fullfile('rtwdemo_basicsc_ert_rtw','myHdrFile.h');
rtwdemodbtype(file,'#ifndef RTW_HEADER_myHdrFile_h_',...
    ' * File trailer for generated code.',1,0)
#ifndef RTW_HEADER_myHdrFile_h_
#define RTW_HEADER_myHdrFile_h_
#include "rtwtypes.h"

typedef real32_T mySingleAlias;
typedef creal32_T cmySingleAlias;

#endif                                 /* RTW_HEADER_myHdrFile_h_ */

/*

View the file rtwdemo_basicsc.h. The code uses the data type alias mySingleAlias to define the structure field input1, which corresponds to the Inport block labeled In1.

file = fullfile('rtwdemo_basicsc_ert_rtw','rtwdemo_basicsc.h');
rtwdemodbtype(file,'/* External inputs (root inport signals with auto storage) */',...
    '} ExtU_rtwdemo_basicsc_T;',1,1)
/* External inputs (root inport signals with auto storage) */
typedef struct {
  mySingleAlias input1;                /* '<Root>/In1' */
  real32_T input2;                     /* '<Root>/In2' */
  real32_T input3;                     /* '<Root>/In3' */
  real32_T input4;                     /* '<Root>/In4' */
} ExtU_rtwdemo_basicsc_T;

Import Type Definition

When you integrate code generated from a model with code from other sources, to avoid redundant typedef statements, you can import a data type definition from the external code. This example shows how to import your own definition of a data type from a header file that you create.

Use a text editor to create a header file to import. Name the file ex_myImportedHdrFile.h. Place it in your working folder. Copy the following code into the file.

#ifndef HEADER_myImportedHdrFile_h_
#define HEADER_myImportedHdrFile_h_

typedef float myTypeAlias;

#endif 

The code uses the identifier myTypeAlias to create an alias for the data type float. The code also uses a macro guard of the form HEADER_filename_h. When you import a data type definition to integrate generated code with code from other sources, you can use macro guards of this form to prevent unintentional identifier clashes.

At the command prompt, create a Simulink.AliasType object named myTypeAlias that creates an alias for the built-in type single. The Simulink data type single corresponds to the C data type float.

myTypeAlias = Simulink.AliasType('single')
myTypeAlias = 

  AliasType with properties:

    Description: ''
      DataScope: 'Auto'
     HeaderFile: ''
       BaseType: 'single'

Configure the object so that generated code imports the type definition from the header file ex_myImportedHdrFile.h.

myTypeAlias.DataScope = 'Imported';
myTypeAlias.HeaderFile = 'ex_myImportedHdrFile.h';

Open the model rtwdemo_basicsc.

open_system('rtwdemo_basicsc')

In the model, select View > Model Data.

In the model, select the Inport block labeled In1.

Use the Data Type column to set the data type to myTypeAlias.

set_param('rtwdemo_basicsc/In1','OutDataTypeStr','myTypeAlias')

In the model, set Configuration Parameters > Code Generation > System target file to ert.tlc. The code generator honors data type aliases such as mySingleAlias only if you select an ERT-based system target file.

set_param('rtwdemo_basicsc','SystemTargetFile','ert.tlc')

Generate code from the model.

rtwbuild('rtwdemo_basicsc')
### Starting build procedure for model: rtwdemo_basicsc
### Successful completion of build procedure for model: rtwdemo_basicsc

In the code generation report, view the file rtwdemo_basicsc_types.h. The code creates a #include directive for your header file ex_myImportedHdrFile.h.

file = fullfile('rtwdemo_basicsc_ert_rtw','rtwdemo_basicsc_types.h');
rtwdemodbtype(file,'#include "ex_myImportedHdrFile.h',...
    '/* Forward declaration for rtModel */',1,0)
#include "ex_myImportedHdrFile.h"

View the file rtwdemo_basicsc.h. The code uses the data type alias myTypeAlias to define the structure field input1, which corresponds to the Inport block labeled In1.

file = fullfile('rtwdemo_basicsc_ert_rtw','rtwdemo_basicsc.h');
rtwdemodbtype(file,'/* External inputs (root inport signals with auto storage) */',...
    '} ExtU_rtwdemo_basicsc_T;',1,1)
/* External inputs (root inport signals with auto storage) */
typedef struct {
  myTypeAlias input1;                  /* '<Root>/In1' */
  real32_T input2;                     /* '<Root>/In2' */
  real32_T input3;                     /* '<Root>/In3' */
  real32_T input4;                     /* '<Root>/In4' */
} ExtU_rtwdemo_basicsc_T;

Display Base Data Types and Aliases on Model Diagram

When you display signal data types on the model diagram by selecting Display > Signals and Ports > Port Data Types, by default, the diagram displays aliases (such as myTypeAlias) instead of base data types (such as int16). To display the base types, choose an option for Display > Signals and Ports > Port Data Type Display Format. For more information, see docid:simulink_ug.f15-90122.