Mapping MATLAB Types to Types in Generated Code

The code generator produces data types in C/C++ that correspond to the data types that you use in your MATLAB® code. The data types that are generated depend on the target platform and compiler. The code generator can produce either built-in C data types, such as short, long, int, and so on, or custom data types defined by using C typedef statements. By default, the code generator produces built-in types for standalone code (lib, dll, or exe) and custom types for MEX code. To use built-in C types, modify the DataTypeReplacement property of the code generation configuration object or use the MATLAB Coder™ App. For more information, see Specify Data Types Used in Generated Code.

To produce custom C/C++ types, the code generator uses predefined data types in the header file tmwtypes.h, located in fullfile(matlabroot,'extern','include'). The code generator can also produce custom data types based on analysis of your MATLAB code. Custom data types are defined in the files rtwtypes.h and myFunction_types.h located in the code generation directory. myFunction is the name of your top-level function. The code generator cannot produce code for every data type that exists within MATLAB. See MATLAB Language Features Supported for C/C++ Code Generation.

When you do not use built-in C data types, the code generator produces these data types:

MATLAB Data TypeCorresponding Custom C/C++ Data Type
complexSee Complex Types.
structSee Structure Types.
fiSee Fixed-Point Types.

When a variable is passed by reference, the corresponding custom data type uses the dereference operator. For example, the corresponding custom C/C++ data type for int8 when passed by reference is int8_T*.

Dynamically allocated arrays map to a custom emxArray_ type. For example, a dynamically allocated char array maps to a type of emxArray_char_T. A dynamically allocated double array maps to the type emxArray_real_T. Dynamic allocation occurs, for example, when array size is not known at compile time or when you create a variable-size array by using coder.varsize without specifying explicit upper bounds. For more information on variable-size arrays, see Use C Arrays in the Generated Function Interfaces.

Complex Types

In MATLAB, complexity is defined as a property of a data type. This table lists the predefined data types that the code generator uses for MATLAB complex data types.

MATLAB Complex Data TypeCorresponding Custom C/C++ Data Type

The code generator defines each complex value as a structure with a real component re and an imaginary component im. For example, see the typedef for creal32_T from tmwtypes.h:

typedef struct {
  real32_T re;/* Real component*/
  real32_T im;/* Imaginary component*/
} creal32_T;
Suppose you define a variable x of type creal32_T. The generated code accesses the real component as and the imaginary component as

If your C/C++ library requires a different representation, you can define your own versions of MATLAB Coder complex types, for example, by using coder.cstructname. However, you must use the names re for the real components and im for the imaginary components in your definitions.

For more information, see Code Generation for Complex Data.

Structure Types

MATLAB Coder maps structures to C/C++ types field-by-field. The order of the structure fields in the MATLAB definition is preserved. To control the name of the generated C/C++ structure type, or provide a definition, use the coder.cstructname function. If you are not using dynamic memory allocation, arrays in structures translate into single-dimension arrays, not pointers. For more information, see Structures.

Fixed-Point Types

The numerictype properties of a fi object determine its C/C++ data type. By default, the code generator tries to use built-in C/C++ types. However, you can choose to use custom C/C++ data types instead. The following table shows how the Signedness, WordLength, and FractionLength properties determine the custom C/C++ data type. The custom C/C++ data type is the next larger target word size that can store the fixed-point value, based on its word length. The sign of the integer type matches the sign of the fixed-point type.

SignednessWord LengthFraction LengthCorresponding Custom C/C++ Data Type
1 1310int16_T

Character Vectors

The MATLAB Coder software maps MATLAB character vectors to C/C++ character arrays. These character arrays are not C/C++ strings because they are not null-terminated. If you pass a MATLAB character vector to external C/C++ code that expects a C/C++ string, the generated C/C++ character array must be null-terminated. To generate a null-terminated C/C++ character array, append a zero to the end of the MATLAB character vector. For example, ['sample text' 0]. Otherwise, generated code that expects a string can stop working without compiler errors or warnings.

Multiword Types

Multiword types are custom types that are generated when the target hardware cannot store your MATLAB data type in a built-in C/C++ type. Multiword types are generated as C/C++ structure types that contain an array of integers. The array dimensions depend on the size of the widest integer type on the target hardware.

For example, for a 128-bit fixed-point type, if the widest integer type on the target hardware is 32-bits, the software generates a structure with an array of four 32-bit integers.

typedef struct
  unsigned int  chunks[4];
} uint128m_T;

If the widest integer type on the target hardware is a long with a size of 64-bits, the code generator produces a structure with an array of two 64-bit long types.

typedef struct
  unsigned long chunks[2];
} uint128m_T;

The C/C++ data type generated from a 64-bit integer MATLAB type depends on the sizes of the integer types on the target hardware. If a built-in type wide enough to store 64-bits does not exist, then the 64-bit MATLAB Coder type maps to a custom multiword type.

See Also


Related Topics