C++ to MATLAB Data Type Mapping - MATLAB & Simulink (original) (raw)

These tables show how MATLAB® converts C/C++ data into equivalent MATLAB data types. MATLAB uses these mappings when creating library definition files. Use this information to help you define missing information for MATLAB signatures.

Numeric Types

Scalar Integer Types

Fixed-width integer data type mappings are independent of the platform and compiler. The mappings for non-fixed-width integer types are based on the compiler.

Fixed-Width Integer Types. For these types, specify DIRECTION as "input" and SHAPE as 1.

C Fixed-Width Scalar Integer Type Equivalent MATLAB Type
int8_t int8
std::complex<int8_t> complex int8
uint8_t uint8
std::complex<uint8_t> complex uint8
int16_t int16
std::complex<int16_t> complex int16
uint16_t uint16
std::complex<uint16_t> complex uint16
int32_t int32
std::complex<int32_t> complex int32
uint32_t uint32
std::complex<uint32_t> complex uint32
int64_t int64
std::complex<int64_t> complex int64
uint64_t uint64
std::complex<uint64_t> complex uint64

Non-Fixed-Width Integer Types. MATLAB supports these non-fixed-width C integer types. Based on the compiler used, MATLAB maps these types to the corresponding fixed-width C types, as shown in the Fixed-Width Integer Types table.

Vector Integer Types

This table shows how MATLAB data types correspond to std::vector types. By default, MATLAB represents std::vector types with the MATLABclib.array type. For more information, see Represent C++ Arrays Using MATLAB Objects.

MATLAB converts the names of fundamental C++ types to upper camel case. For complex types, MATLAB adds std.complex to the clib.array type.

For these types, specify DIRECTION as "input" and SHAPE as 1.

C++ Parameter TInteger Typefor std::vector Equivalent MATLABclib.array Typefor Interface_lib_ Element Type
std::vector<int8_t> clib.array.lib.SignedChar clib.lib.SignedChar
std::vector<std::complex<int8_t>> clib.array.lib.std.complex.SignedChar clib.lib.std.complex.SignedChar
std::vector<uint8_t> clib.array.lib.UnsignedChar clib.lib.UnsignedChar
std::vector<std::complex<uint8_t>> clib.array.lib.std.complex.UnsignedChar clib.lib.std.complex.UnsignedChar
std::vector<int16_t> clib.array.lib.Short clib.lib.Short
std::vector<std::complex<int16_t>> clib.array.lib.std.complex.Short clib.lib.std.complex.Short
std::vector<uint16_t> clib.array.lib.UnsignedShort clib.lib.UnsignedShort
std::vector<std::complex<uint16_t>> clib.array.lib.std.complex.UnsignedShort clib.lib.std.complex.UnsignedShort
std::vector<int32_t> clib.array.lib.Int clib.lib.Int
std::vector<std::complex<int32_t>> clib.array.lib.std.complex.Int clib.lib.std.complex.Int
std::vector<uint32_t> clib.array.lib.UnsignedInt clib.lib.UnsignedInt
std::vector<std::complex<uint32_t>> clib.array.lib.std.complex.UnsignedInt clib.lib.std.complex.UnsignedInt
std::vector<int64_t> clib.array.lib.LongLong clib.lib.LongLong
std::vector<std::complex<int64_t>> clib.array.lib.std.complex.LongLong clib.lib.std.complex.LongLong
std::vector<uint64_t> clib.array.lib.UnsignedLongLong clib.lib.UnsignedLongLong
std::vector<std::complex<uint64_t>> clib.array.lib.std.complex.UnsignedLongLong clib.lib.std.complex.UnsignedLongLong

Floating Point Types

For these types, specify DIRECTION as "input" and SHAPE as 1.

C Floating Point Type Equivalent MATLAB Type
double double
std::complex complex double
float single
std::complex complex single
C++ Parameter TFloating Point Typefor std::vector Equivalent MATLABclib.array Typefor Interface_lib_ Element Typea
std::vector clib.array.lib.Double clib.lib.Double
std::vector<std::complex> clib.array.lib.std.complex.Double clib.lib.std.complex.Double
std::vector clib.array.lib.Float clib.lib.Float
std::vector<std::complex> clib.array.lib.std.complex.Float clib.lib.std.complex.Float
a For information about using element types, see Represent C++ Arrays Using MATLAB Objects.

String and Character Types

These tables show how C++ string and char data types correspond to MATLAB data types. The data mapping depends on how the type is used in the function, as a parameter, return type, or data member (property). For example, these function definitions show different uses of type T.

void fcn(T);  // T is a parameter type (MATLAB input argument)
T fcn();      // T is a return type (MATLAB output argument)

Plain C++ Character and String Types

For these types, specify DIRECTION as "input" and SHAPE as 1.

Plain C++ Character Type Equivalent MATLAB Type
char int8
signed char int8
unsigned char uint8
wchar_t char
char16_t char
char32_t char
Plain C++ String Type Equivalent MATLAB Type
std::string string (MATLAB converts characters to the platform default encoding forstd::string.)
std::wstring string
std::u16string string
std::u32string string

C++ char* and char[] Types

C++ Parameter Type MLTYPE SHAPE
char*a const char* "int8" "char" "clib.array.lib.Char" Scalar valueArray of scalar values
"string" "nullTerminated"
char[] const char[] "int8" "char" "clib.array.lib.Char" Scalar value
"string" "nullTerminated"
wchar_t* const wchar_t* wchar_t[] const wchar_t[] char16_t* const char16_t* char16_t[] const char16_t[] char32_t* const char32_t* char32_t[] const char32_t[] "char" Scalar valueArray of scalar values
"string" "nullTerminated"
a MATLAB sets the DIRECTION ofchar* parameter types to "input". To define a char* argument as a string output, setDIRECTION to "output" and use theNumElementsInBuffer name-value argument. For an example, see the getMessage function in the Define String Argument table.
C++ Return Type MLTYPE SHAPE
char* const char* "string" (default)To delete the character buffer, specify a deleter function using theDeleteFcn argument in the defineOutput function. "nullTerminated"
"int8" Scalar valueArray of scalar values
wchar_t* const wchar_t* wchar_t[] const wchar_t[] char16_t* const char16_t* char16_t[] const char16_t[] char32_t* const char32_t* char32_t[] const char32_t[] "string" (default)To delete the character buffer, specify a deleter function using theDeleteFcn argument in the defineOutput function. "nullTerminated"
"char" Scalar valueArray of scalar values
C++ Data Member Type Equivalent MATLAB Type
char* char[] string clib.array.lib.Char int8 char
wchar_t* char16_t* char32_t* string char

C++ Array of Strings

C++ Array of String Parameter Type MLTYPE SHAPEa
char** const char** char*[] const char*[] wchar_t** const wchar_t** wchar_t*[] const wchar_t*[] char16_t** const char16_t** char16_t*[] const char16_t*[] char32_t** const char32_t** char32_t*[] const char32_t*[] "string" {scalar value,"nullTerminated"}{parameter name,"nullTerminated"}
a 1D array of string. The first element is the size of array and the last element is the shape of each element.
C++ Array of String Data Member Type Equivalent MATLAB Type
char*[] and const char*[] wchar_t*[] andconst wchar_t*[] char16_t*[] andconst char16_t*[] char32_t*[] andconst char32_t*[] string

MATLAB does not support these const and non-const C++ return types.

std::vector<T> String Types

For these types, specify DIRECTION as"input".

C++ std::vector String Type Equivalent MATLABclib.array Typefor Interface_lib_ Element Typea
std::vectorstd::char clib.array.lib.std.Char clib.lib.std.Char
std::vectorstd::string clib.array.lib.std.String clib.lib.std.String
std::vectorstd::wstring clib.array.lib.std.wString clib.lib.std.wString
std::vectorstd::u16string clib.array.lib.std.u16String clib.lib.std.u16String
std::vectorstd::u32string clib.array.lib.std.u32String clib.lib.std.u32String
a For information about using element types, see Represent C++ Arrays Using MATLAB Objects.

bool Types

For these types, specify DIRECTION as "input" andSHAPE as 1.

bool Type Equivalent MATLAB Type
bool logical

Static Data Members

MATLAB treats public static and public const static data members as read-only properties. You cannot update the value of a static data member. For example, build an interface lib to this header file.

class Set
{
   public:
   static int p1;
   const static int p2;
};
int Set::p1 = 5;
const int Set::p2 = 10;

The help for clib.lib.Set shows p1 andp2 as properties. To use the properties in MATLAB:

res = clib.lib.Set.p1 + clib.lib.Set.p2

Do not update a static property by using the class name. If you do, then MATLAB creates a structure named clib. You must manually clear theclib structure before calling any functions in the interface. For example, if you try to update static property p1, thenclib is a variable:

Clear the variable before calling any commands in clib.lib.

clear clib clib.lib.Set.p1

For information about using static properties as SHAPE arguments, seeUse Property or Method as SHAPE.

const Types

MATLAB treats an object of a C++ read-only class as a const object. The name of the object is the C++ class name, and MATLAB adds "read-only" text to the object display. For example, create a MATLAB interface to this library MyClass:

const class MyClass 
{ 
    public: 
        int val1; 
        int val2; 
}; 
 
const MyClass& func(); 

Call the function func in MATLAB to create a const MyClass object:

res = clib.MyClass.func()

res =
read-only MyClass with properties: val1: 1 val2: 2

To check if the object is const, call clibIsReadOnly.

The function returns logical 1 (true) if the object is read-only. Otherwise, it returns logical 0 (false).

User-Defined Types

Class and Struct Types

In some situations, you can pass MATLAB struct arrays as C++ struct array arguments. For more information, see Supported struct Types.

This table shows how to configure a C++ parameter type T in the definition file for a MATLAB interface to library libname, whereT is a class or a struct.

C++ Parameter Type Equivalent MATLAB Type in libname DIRECTION SHAPE
T clib.libname.T "input" 1
T* clib.libname.T "input" 1
clib.array.libname.T "input" 1Fixed dimensions: Enter a numerical array, such as [5,2].Variable dimensions: Enter a string array of parameter names, such as ["row","col"].
T[] clib.array.libname.T "input" 1Fixed dimensionsVariable dimensions
T& clib.libname.T "input" 1
T** clib.libname.T "output" 1
std::vector clib.array.libname.T "input" 1
std::shared_ptr clib.libname.T "input" 1
std::shared_ptr& clib.array.libname.T "input" "inputoutput" 1

This table shows how to configure a C++ return type T.

C++ Return Type Equivalent MATLAB Type in libname SHAPE
T clib.libname.T 1
T* clib.libname.T 1
clib.array.libname.T 1Fixed dimensions: Enter a numerical array, such as [5,2].Variable dimensions: Enter a string array of parameter names, such as ["row","col"].
T[] clib.array.libname.T 1Fixed dimensionsVariable dimensions
T& clib.libname.T 1
std::vector clib.array.libname.T 1
std::shared_ptr clib.libname.T 1
std::shared_ptr& clib.array.libname.T 1

This table shows how to configure a C++ data member type T.

C++ Data Member Type Equivalent MATLAB Type in libname SHAPE
T clib.libname.T 1
T* clib.libname.T 1
clib.array.libname.T 1Fixed dimensions: Enter a numerical array, such as [5,2].Variable dimensions: Enter a string array of parameter names, such as ["row","col"].
T[] clib.array.libname.T 1Fixed dimensionsVariable dimensions
std::vector clib.array.libname.T 1
std::shared_ptr clib.libname.T 1

Supported struct Types

Since R2024b

MATLAB supports C++ structures, including nested structures, that meet these conditions:

The structure does not contain:

For information about passing a MATLAB structure as a C++ struct argument, see Pass struct Parameter.

Enumerated Types

This table shows how to configure a C++ enum type T in the definition file for a MATLAB interface to library libname, whereT is an enumerated type.

C++ Parameter Type Equivalent MATLAB Type in libname DIRECTION SHAPE
T clib.libname.T "input" 1
C++ Return Type Equivalent MATLAB Type in libname SHAPE
T clib.libname.T 1
C++ Data Member Type Equivalent MATLAB Type in libname SHAPE
T clib.libname.T 1

nullptr Argument Types

nullptr Input Argument Types

MATLAB provides a clib.type.nullptr type so that you can pass NULL to a function with these C++ input argument types:

The clib.type.nullptr type is supported for these MATLAB argument types:

nullptr Return Types

The C++ interface returns type-specific empty values for functions that return nullptr.

void* Argument Types

To pass void* arguments to and from C++ functions, see Define void* and void** Arguments. MATLAB does not support void* data members.

When passing a void* input argument, MATLAB converts the underlying data to the corresponding C++ type.

Fundamental Types Mapping

C++ Type Equivalent MATLABvoid* Type
int8_t* int8
uint8_t* uint8
int16_t* int16
uint16_t* uint16
int32_t* int32
uint32_t* uint32
int64_t* int64
uint64_t* uint64
float* single
double* double
bool* logical

clib.array Types Mapping

C++ Type Equivalent MATLABclib.array Typea for libname
char* clib.array.libname.Char
signed char* clib.array.libname.SignedChar
unsigned char* clib.array.libname.UnsignedChar
short* clib.array.libname.Short
unsigned short* clib.array.libname.UnsignedShort
int* clib.array.libname.Int
unsigned int* clib.array.libname.UnsignedInt
long* clib.array.libname.Long
unsigned long* clib.array.libname.UnsignedLong
long long* clib.array.libname.LongLong
unsigned long long* clib.array.libname.UnsignedLongLong
float* clib.array.libname.Float
double* clib.array.libname.Double
bool* clib.array.libname.Bool
a MATLAB converts the names of fundamental C++ types to upper camel case.

typedef void* Mapping

C++ Type Equivalent MATLAB Type for libname
typedef void* Handle clib.libname.Handle

Unsupported Data Types

If the data type of an argument/return type in a class constructor, method, or function is one of these types, or if the library contains any unsupported language features, then the functionality is not included in the MATLAB interface to the library.

Messages About Unsupported Types

MATLAB reports on constructs that use unsupported types. To view these messages, use the Verbose option in the clibgen.generateLibraryDefinition or clibgen.buildInterface functions.

For example, suppose that functionName inClassName is defined in HeaderFile.h. If an argument to functionName is of unsupported type_type_, then MATLAB does not add functionName to the definition file. In addition, if Verbose is true, thenclibgen.generateLibraryDefinition displays this message.

Did not add member 'functionName' to class 'ClassName' at HeaderFile.h:290.   'type' is not a supported type.

See Also

clibgen.generateLibraryDefinition | clibgen.buildInterface | clibIsReadOnly