coder.ReplacementTypes - Configuration parameter to specify custom names for MATLAB built-in data types in C/C++ code generation - MATLAB (original) (raw)
Configuration parameter to specify custom names for MATLAB built-in data types in C/C++ code generation
Description
Creation
Use the coder.config function to create acoder.EmbeddedCodeConfig
object for generation of standalone code. When the coder.config
function creates acoder.EmbeddedCodeConfig
object, it sets theReplacementTypes
property to coder.ReplacementTypes
object.
Properties
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.
Value | Description |
---|---|
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_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.
Value | Description |
---|---|
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 inReplacementTypes.HeaderFiles. For example,cfg.ReplacementTypes.HeaderFiles = {'myHeader1.h','myHeader2.h','myHeader3.h'}. |
Examples
Write a MATLAB function from which you can generate code. This example uses the functionmyAdd.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
totrue
.
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 asCustom_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 inmyFiles
. 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 inmyFiles
.
#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 asCustom_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 thecoder.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. TheCustom Data Type Replacement table lists the name of the supported data types. Specify your custom names for these data types and pressEnter.
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, add the external header file names. For more information, see Import Custom Data Type Definitions from External Header Files (Embedded Coder).
See Specify Code Configuration Parameters Interactively.
Version History
Introduced in R2019b
If you try to use semicolons in a character vector to separate multiple filenames for the HeaderFiles
property of a coder.ReplacementTypes
object, the code generator produces an error. Use string arrays or cell arrays of character vectors instead. For example:
- Use a string array —
cfg.ReplacementTypes.HeaderFiles = ["myHeader1.h","myHeader2.h","myHeader3.h"]
- Use a cell array of character vectors —
cfg.ReplacementTypes.HeaderFiles = {'myHeader1.h','myHeader2.h','myHeader3.h'}
Using semicolons in a character vector to separate multiple filenames for theHeaderFiles
property of a coder.ReplacementTypes
object produces a warning and will be removed in a future release. Use string arrays or cell arrays of character vectors instead. For example:
- Use a string array —
cfg.ReplacementTypes.HeaderFiles = ["myHeader1.h","myHeader2.h","myHeader3.h"]
- Use a cell array of character vectors —
cfg.ReplacementTypes.HeaderFiles = {'myHeader1.h','myHeader2.h','myHeader3.h'}
In a future release, specifying multiple file names, paths, or reserved names in code configuration objects by using character vectors or string scalars that have delimiters will be removed. Use string arrays and a cell array of character vector instead. For example, to include multiple header file names, you can use either a string array inReplacementTypes.HeaderFiles
ascfg.ReplacementTypes.HeaderFiles = ["myHeader1.h","myHeader2.h","myHeader3.h"]
or a cell array of character vectors as cfg.ReplacementTypes.HeaderFiles = {'myHeader1.h','myHeader2.h','myHeader3.h'}
.