Main Content

C++ to MATLAB Data Type Mapping

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

Fixed-Width Integer Data Types

These type mappings are independent of platform and compiler. For integer types based on the compiler, see Non-Fixed-Width Integer Types.

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

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

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 Numeric Types Fixed-Width Integer 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

std::vector<T> Integer Types

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.

For these types, specify DIRECTION as "input" and SHAPE as 1. For information about using element types, see MATLAB Object For C++ Arrays.

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

Equivalent MATLAB clib.array Type for libname

Element Type

std::vector<int8_t>

clib.array.libname.SignedChar

clib.libname.SignedChar

std::vector<uint8_t>

clib.array.libname.UnsignedChar

clib.libname.UnsignedChar

std::vector<int16_t>

clib.array.libname.Short 

clib.libname.Short 

std::vector<uint16_t>

clib.array.libname.UnsignedShort 

clib.libname.UnsignedShort 

std::vector<int32_t>

clib.array.libname.Int

clib.libname.Int

std::vector<uint32_t>

clib.array.libname.UnsignedInt

clib.libname.UnsignedInt

std::vector<int64_t>

clib.array.libname.LongLong 

clib.libname.LongLong 

std::vector<uint64_t>

clib.array.libname.UnsignedLongLong 

clib.libname.UnsignedLongLong 

Floating Point Types

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

C Floating Point TypeEquivalent MATLAB Type

float

single

double

double

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

Equivalent MATLAB clib.array Type for libname

Element Type[a]

std::vector<float>

clib.array.libname.Single

clib.libname.Single

std::vector<double>

clib.array.libname.Double

clib.libname.Double

[a]  For information about using element types, see MATLAB Object For C++ Arrays.

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 an input argument, output argument, or data member. For example, these function definitions show different uses of type T.

void fnc(T);  // T is a parameter type / input arugment
T fnc();      // T is a return type / output argument

Plain C++ Character and String Types

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

Plain C++ Character TypeEquivalent MATLAB Type

char

int8

signed char

int8

unsigned char

uint8

wchar_t

char

char16_t

char

char32_t

char

Plain C++ String TypeEquivalent MATLAB Type

std::string

string
(MATLAB converts characters to the platform default encoding for std::string.)

std::wstring

string

std::u16string

string

std::u32string

string

C++ char* Types

C++ char* Input Parameter TypeMLTYPESHAPE

char*

"int8"

  • Scalar value

  • Array of scalar values

wchar_t*
char16_t*
char32_t*
"char"
  • Scalar value

  • Array of scalar values

C++ char* Return TypeMLTYPESHAPE

char*

"string"

"nullTerminated"

  • "clib.array.lib.Char"

  • "int8"

  • "char"

  • Scalar value

  • Array of scalar values

wchar_t*
char16_t*
char32_t*

"string"

"nullTerminated"

"char"

  • Scalar value

  • Array of scalar values

C++ char* Data Member TypeEquivalent MATLAB Type

char*

  • string

  • clib.array.lib.Char

  • int8

  • char

wchar_t*
char16_t*
char32_t*

  • string

  • char

C++ const char* Types

C++ const char* TypesEquivalent MATLAB Type

const char*

  • string

  • clib.array.lib.Char

  • int8

  • char

wchar_t*
char16_t*
char32_t*

  • string

  • char

C++ char** Types

MATLAB does not support C++ char** return types.

C++ char** Input Parameter TypeMLTYPESHAPE[a]
char**"string"[scalar value,"nullTerminated"]
char*[]"string"[scalar value,"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++ char** Data Member TypeEquivalent MATLAB Type
char**

string

char*[]

string

C++ const char** Types

MATLAB does not support C++ const char** return types.

C++ const char** Input Parameter TypeMLTYPESHAPE[a]
const char**

"string"

[scalar value,"nullTerminated"]
const char*[]

"string"

[scalar value,"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++ const char** Data Member TypeEquivalent MATLAB Type
const char**

string

const char*[]

string

std::vector<T> String Types

For these types, specify DIRECTION as "input".

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

Equivalent MATLAB clib.array Type for libname

Element Type[a]

std::vector<std::string>

clib.array.libname.std.String 

clib.libname.std.String 

std::vector<std::wstring>

clib.array.libname.std.wString 

clib.libname.std.wString 

std::vector<std::u16string>

clib.array.libname.std.u16String 

clib.libname.std.u16String 

std::vector<std::u32string>

clib.array.libname.std.u32String 

clib.libname.std.u32String 

[a]  For information about using element types, see MATLAB Object For C++ Arrays.

bool Types

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

bool TypeEquivalent MATLAB Type

bool

logical

std::vector<T> bool Type

Equivalent MATLAB clib.array Type for libname

Element Type[a]

bool

clib.array.libname.Bool

clib.libname.Bool

[a]  For information about using element types, see MATLAB Object For C++ Arrays.

User-Defined Types

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

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

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.

void* Argument Types

To pass void* arguments to and from C++ functions, see Use 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 MATLAB void* 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 MATLAB clib.array Type 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

Types for C++ Classes and Structs in Library

C++ Type

Equivalent MATLAB Type for libname

MyClassclib.libname.MyClass

typedef void* Mapping

C++ Type

Equivalent MATLAB Type for libname

typedef void* Handleclib.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.

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

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

  • References to a pointer, for example int*&

  • Pointers or arrays of std::string

  • Pointers or references to enumerations

  • Reference data members

  • void* data members

  • std smart pointers other than std::shared_ptr

  • Static data members

  • ** pointers, except:

    • MATLAB supports char**

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

  • Multilevel pointers, such as type***

  • C function pointers and std::function as function return types or data members. You also cannot pass a MATLAB function as input to C function pointers or std::function parameter.

  • Class templates with incomplete or no instantiations

  • union

  • Types defined in the std namespace, except these supported types:

    • std::string

    • std::wstring

    • std::u16string

    • std::u32string

    • std::vector

    • std::shared_ptr

    • std::function

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