MATLAB to C++ Data Type Mapping

Fixed-Width Data Types

These tables show how MATLAB® data types correspond to fixed-width C++ types.

C Fixed-Width Integer TypeEquivalent MATLAB 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

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

C++ std::vector<T> Type

Equivalent MATLAB clib.array Type for libname

std::vector<int8_t>

clib.array.libname.SignedChar

std::vector<uint8_t>

clib.array.libname.UnsignedChar

std::vector<int16_t>

clib.array.libname.Short 

std::vector<uint16_t>

clib.array.libname.UnsignedShort 

std::vector<int32_t>

clib.array.libname.Int

std::vector<uint32_t>

clib.array.libname.UnsignedInt

std::vector<int64_t>

clib.array.libname.LongLong 

std::vector<uint64_t>

clib.array.libname.UnsignedLongLong 

C Floating Point TypeEquivalent MATLAB Type

float

single

double

double

C++ std::vector<T> Floating Point Type

Equivalent MATLAB clib.array Type for libname

std::vector<float>

clib.array.libname.Single

std::vector<double>

clib.array.libname.Double

Other C TypeEquivalent MATLAB Type

bool

logical

Other C++ std::vector<T> Type

Equivalent MATLAB clib.array Type for libname

bool

clib.array.libname.Bool

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 Data Types tables.

  • short

  • short int

  • signed short

  • signed short int

  • unsigned short

  • unsigned short int

  • int

  • signed int

  • unsigned

  • unsigned int

  • long

  • signed long

  • signed long int

  • unsigned long

  • unsigned long int

  • long long

String and Character Types

These tables show how MATLAB converts string and char data returned from C++ into MATLAB types.

C++ Character TypeEquivalent MATLAB Type

char

int8

signed char

int8

unsigned char

uint8

wchar_t

char

C++ String TypeEquivalent MATLAB Type

std::string

string, ASCII characters only

std::wstring
std::u16string
std::u32string

string

char *

string

const char * Character and String TypesEquivalent MATLAB Type

const char * input argument

Same as char *:

  • clib.array.lib.Char

  • int8

  • string

  • char

const char * return argument

scalar int8

C++ Exact-Width Character TypeEquivalent MATLAB Type

char16_t

char

char32_t

char

User-Defined Types

These tables show how to call a C++ user-defined type in a MATLAB interface library libname.

C++ User-Defined TypeEquivalent MATLAB Type

struct StructName

libname.StructName

enum EnumName

libname.EnumName

C++ Class Type

Equivalent MATLAB clib.array Type for libname

std::vector<T>

clib.array.libname.T

std::vector<T []>

clib.libname.T

std::shared_ptr<T>

clib.array.libname.T

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:

  • Pointers to objects. However, pointers to fundamental MATLAB array types are not supported.

  • shared_ptr

  • Arrays

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

  • scalar object pointers

  • clib arrays

nullptr Return Types

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

  • For type double,​ MATLAB returns [] for the value double.empty.

  • For all other fundamental types,​ MATLAB returns an MLTYPE.empty value. To determine MLTYPE, look for the C or C++ type in the tables in this topic. MLTYPE is in the Equivalent MATLAB Type column.

    To test for nullptr types, call the isempty function.

  • For non-fundamental types,​ MATLAB returns a nullptr object. To test for nullptr objects, call the clibIsNull function.

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.

  • Any type with a size greater than 64 bits, for example long double

  • void *

  • Arrays of character types (wchar_t, char16_t, char32_t)

  • Pointers or arrays of std::string

  • References to a pointer in a function

  • Pointers or references to enumerations

  • Reference data members

  • std smart pointers other than std::shared_ptr

  • Static data members

  • ** pointers to primitive types, such as double**. MATLAB supports ** pointers to custom classes used as function or ​method parameter types.

  • Multilevel pointers, such as type***

  • Function pointer

  • Class templates with incomplete or no instantiations

  • union

These types defined in the std namespace are supported. All other types in the std namespace are not supported.

  • std::string, std::wstring, std::u16string, std::u32string

  • std::vector

  • std::shared_ptr

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 in ClassName 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, then clibgen.generateLibraryDefinition displays this message.

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

See Also

|

Related Topics