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

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 TypeEquivalent 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.

  • 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

Vector 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.

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 T
Integer Type
for std::vector<T>

Equivalent MATLAB clib.array Type
for 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 TypeEquivalent MATLAB Type

double

double

std::complex<double>

complex double

float

single

std::complex<float>

complex single

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

Equivalent MATLAB clib.array Type
for Interface lib

Element Typea

std::vector<double>

clib.array.lib.Double

clib.lib.Double

std::vector<std::complex<double>>

clib.array.lib.std.complex.Double

clib.lib.std.complex.Double

std::vector<float>

clib.array.lib.Float

clib.lib.Float

std::vector<std::complex<float>>

clib.array.lib.std.complex.Float

clib.lib.std.complex.Float

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 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 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* and char[] Types

C++ Parameter TypeMLTYPESHAPE

char*a
const char*

"int8"
"char"
"clib.array.lib.Char"

Scalar value
Array 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 value
Array of scalar values

"string"

"nullTerminated"

a MATLAB sets the DIRECTION of char* parameter types to "input". To define a char* argument as a string output, set DIRECTION to "output" and use the NumElementsInBuffer name-value argument. For an example, see the getMessage function in the Define String Argument table.

C++ Return TypeMLTYPESHAPE

char*
const char*

"string" (default)
To delete the character buffer, specify a deleter function using the DeleteFcn argument in the defineOutput function.

"nullTerminated"

"int8"

Scalar value
Array 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 the DeleteFcn argument in the defineOutput function.

"nullTerminated"

"char"

Scalar value
Array of scalar values

C++ Data Member TypeEquivalent 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 TypeMLTYPESHAPEa

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 TypeEquivalent MATLAB Type

char*[] and const char*[]
wchar_t*[] and const wchar_t*[]
char16_t*[] and const char16_t*[]
char32_t*[] and const char32_t*[]

string

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

  • char** and char*[]

  • wchar_t** and wchar_t*[]

  • char16_t** and char16_t*[]

  • char32_t** and char32_t*[]

std::vector<T> String Types

For these types, specify DIRECTION as "input".

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

Equivalent MATLAB clib.array Type
for Interface lib

Element Typea

std::vector<std::char>

clib.array.lib.std.Char 

clib.lib.std.Char 

std::vector<std::string>

clib.array.lib.std.String 

clib.lib.std.String 

std::vector<std::wstring>

clib.array.lib.std.wString 

clib.lib.std.wString 

std::vector<std::u16string>

clib.array.lib.std.u16String 

clib.lib.std.u16String 

std::vector<std::u32string>

clib.array.lib.std.u32String 

clib.lib.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 lib

Element Typea

bool

clib.array.lib.Bool

clib.lib.Bool

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

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 and p2 as properties. To use the properties in MATLAB:

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

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 the clib structure before calling any functions in the interface. For example, if you try to update static property p1, then clib is a variable:

clib.lib.Set.p1 = 20
clib = 
    lib: [1×1 struct]

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

clear clib
clib.lib.Set.p1
ans = 5

For information about using static properties as SHAPE arguments, see Use 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.

clibIsReadOnly(res)
ans = 
  logical   
   1

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

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

C++ Parameter Type

Equivalent MATLAB Type in libnameDIRECTIONSHAPE

T

clib.libname.T

"input"

1

T*clib.libname.T"input"

1

clib.array.libname.T"input"

1
Fixed 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"

1
Fixed dimensions
Variable dimensions

T&

clib.libname.T

"input"

1

T**

clib.libname.T

"output"

1

std::vector<T>

clib.array.libname.T

"input"

1

std::shared_ptr<T>

clib.libname.T

"input"

1

std::shared_ptr<T>&

clib.array.libname.T

"input"
"inputoutput"

1

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

C++ Return TypeEquivalent MATLAB Type in libnameSHAPE

T

clib.libname.T

1

T*clib.libname.T

1

clib.array.libname.T

1
Fixed 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

1
Fixed dimensions
Variable dimensions

T&

clib.libname.T

1

std::vector<T>

clib.array.libname.T

1

std::shared_ptr<T>

clib.libname.T

1

std::shared_ptr<T>&

clib.array.libname.T

1

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

C++ Data Member TypeEquivalent MATLAB Type in libnameSHAPE

T

clib.libname.T

1

T*clib.libname.T

1

clib.array.libname.T

1
Fixed 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

1
Fixed dimensions
Variable dimensions

std::vector<T>

clib.array.libname.T

1

std::shared_ptr<T>

clib.libname.T

1

Enumerated Types

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

C++ Parameter Type

Equivalent MATLAB Type in libnameDIRECTIONSHAPE
T

clib.libname.T

"input"

1

C++ Return TypeEquivalent MATLAB Type in libnameSHAPE
T

clib.libname.T

1

C++ Data Member TypeEquivalent MATLAB Type in libnameSHAPE
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:

  • 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 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 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 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* 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

  • References to a pointer, for example int*&

  • Pointers or arrays of std::string

  • Pointers or references to enumerations

  • Vectors of pointers to class objects, for example std::vector<Myclass*>

  • Reference data members

  • void* data members

  • Multidimensional data member arrays

  • Modifying static data members

  • ** pointers, except:

    • MATLAB supports char** types.

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

    • MATLAB supports void** 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

    • std::complex

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