Main Content

coder.ReplacementTypes

Configuration parameter to specify custom names for MATLAB built-in data types in C/C++ code generation

Since R2019b

Description

A coder.ReplacementTypes object contains the configuration parameters that the code generator uses for creating custom data type names for MATLAB® built-in data types in C/C++ code generation.

You must associate coder.ReplacementTypes object with an Embedded Coder® configuration object (a coder.EmbeddedCodeConfig object) that you pass to the codegen function.

You can access coder.ReplacementTypes properties from either the command-line interface (see Specify Custom Names for MATLAB Built-in Data Types) or a dialog box for the associated configuration object (see Access Replacement Types Properties Through a Dialog Box).

Creation

Use the coder.config function to create a coder.EmbeddedCodeConfig object for generation of standalone code. When the coder.config function creates a coder.EmbeddedCodeConfig object, it sets the ReplacementTypes property to coder.ReplacementTypes object.

Properties

expand all

Custom name for a double data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a single data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a uint8 data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a uint16 data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a uint32 data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a uint64 data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a int8 data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a int16 data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a int32 data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a int64 data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a char data type in the generated C/C++ code, specified as a character vector or string scalar.

Custom name for a logical data type in the generated C/C++ code, specified as a character vector or string scalar.

Enable or disable importing type definitions from external header files for use in the generated C/C++ code.

ValueDescription
false

This value is the default value.

Custom type definitions are generated in the file rtwtypes.h. Importing type definitions from external header files is not allowed for code generation.

true

Importing type definitions from external header files is allowed for code generation. The specified header files are included in <function>_types.h. For example, if the function name is myFunc, the external header file is included in a generated header file myFunc_types.h.

External header file names that contain custom type definitions.

To specify a single header file name, you can use a character vector or a string scalar.

To specify multiple header files, use one of the values in this table.

ValueDescription
String array

A string array in ReplacementTypes.HeaderFiles. For example, cfg.ReplacementTypes.HeaderFiles = ["myHeader1.h","myHeader2.h","myHeader3.h"].

Cell array of character vectors

A cell array of character vectors in ReplacementTypes.HeaderFiles. For example, cfg.ReplacementTypes.HeaderFiles = {'myHeader1.h','myHeader2.h','myHeader3.h'}.

Examples

collapse all

Write a MATLAB function from which you can generate code. This example uses the function myAdd.m, which returns the sum of its inputs.

function c = myAdd(a,b)
c = a + b;
end

Create a coder.EmbeddedCodeConfig object for generation of a static library.

cfg = coder.config('lib','ecoder',true);

Set the EnableCustomReplacementTypes to true.

cfg.EnableCustomReplacementTypes = true;

Specify custom names for MATLAB built-in data types. For example, in the code, double is named as Custom_Double and int8 is named as Custom_Int8.

cfg.ReplacementTypes.double = "Custom_Double";
cfg.ReplacementTypes.int8 = "Custom_Int8";

Generate code by using the codegen function and the -config option.

codegen myAdd.m -args {1,int8(1)} -config cfg -report

The generated code contains custom data type names.

Create a writable folder myFiles.

Write a MATLAB function from which you can generate code. Save the function in myFiles. This example uses the function myAdd.m, which returns the sum of its inputs.

function c = myAdd(a,b)
c = a + b;
end

Write your header file myHeader.h that contains type definitions for the two inputs of the function myAdd.m. Save it in myFiles.

#if !defined(MYHEADER)
#define MYHEADER
typedef double Custom_Double;
typedef char Custom_Int8;
#endif

Create a coder.EmbeddedCodeConfig object for generation of a static library.

cfg = coder.config('lib','ecoder',true);

Specify custom names for MATLAB built-in data types. For example, in the code, double is named as Custom_Double and int8 is named as Custom_Int8.

cfg.EnableCustomReplacementTypes = true;
cfg.ReplacementTypes.double = "Custom_Double";
cfg.ReplacementTypes.int8 = "Custom_Int8";

Specify configuration properties for importing external header files.

% Include single header file

cfg.ReplacementTypes.IsExtern = true;
cfg.ReplacementTypes.HeaderFiles = "myHeader.h";
cfg.CustomInclude = 'C:\myFiles'; % Include path of the header file

% Include multiple header files

cfg.ReplacementTypes.IsExtern = true;
cfg.ReplacementTypes.HeaderFiles = "myHeader1.h;myHeader2.h;myHeader3.h";
cfg.CustomInclude = '"C:\Program Files\MATLAB\myFiles"'; % Include path of the header files

Generate code by using the codegen function and the -config option.

codegen myAdd.m -args {1,int8(1)} -config cfg -report

In the generated code, myAdd_types.h includes the external header file myHeader.h.

To read more about importing custom data type definitions from external header files, see Import Custom Data Type Definitions from External Header Files (Embedded Coder).

Open the dialog box for the configuration object that refers to the coder.ReplacementTypes object. For example:

cfg = coder.config('lib');
open('cfg');

In the dialog box, click the Code Appearance tab.

Select Enable custom data type replacement. The Custom Data Type Replacement table lists the name of the supported data types. Specify your custom names for these data types and press Enter.

You can import your own custom type definitions from external header files. Select the Import custom types from external header files check box. In the Header files text field, enter a semicolon-separated list of external header file names. For more information, see Import Custom Data Type Definitions from External Header Files (Embedded Coder).

See Specify Configuration Parameters in Command-Line Workflow Interactively.

Version History

Introduced in R2019b

expand all