Contenu principal

Data Type Conversions Between MATLAB REST Function Services and Python

R2026b

When you use a MATLAB® REST function service in your Python® application, the service converts between Python data types and MATLAB data types. For information on data type conversions in Python when calling MATLAB Engine API for Python, see Pass Data Between MATLAB and Python from Python.

Pass Data from Python to MATLAB

When you pass data from Python to MATLAB, the MATLAB REST function service converts the data into the equivalent MATLAB data types.

Python Data Type

Resulting MATLAB Data Type
(scalar unless otherwise noted)

matlab numeric array object

Numeric array
See MATLAB Arrays as Python Variables.

Python objects that support the buffer protocol, such as memoryview, array.array, or NumPy ndarray

This conversion is not supported directly. You must first convert such types to a supported type, such as matlab.double, matlab.single, or one of the MATLAB integer types.

float

double

complex

Complex double

int

int64

float('nan')
numpy.nan

NaN

float('inf')
numpy.inf

Inf

bool

logical

str

char

bytearray

uint8 array

bytes

uint8 array

list

cell array

set

cell array

tuple

cell array

dict

struct
dict keys must meet certain specifications. See Use MATLAB Dictionaries in Python.

Unsupported Python Data Types

Along with the exceptions noted in the table, the following conversions from Python data types to MATLAB data types are not supported.

  • datetime.datetime

  • datetime.timedelta

  • enum type

  • matlab.dictionary

  • matlab.object

  • None object

  • numpy.datetime64

  • numpy.timedelta64

  • pandas.DataFrame

  • Python objects other than dict that support the mapping protocol (such as collections.OrderedDict)

  • Python class (module.type) objects

Pass Data from MATLAB to Python

When you pass data from MATLAB to Python, MATLAB converts the data into the equivalent Python data types.

MATLAB Output Argument Type
(scalar unless otherwise noted)

Resulting Python Data Type

Numeric array

matlab numeric array object
See MATLAB Arrays as Python Variables.

double
single

float

Complex (any numeric type)

complex

int8, int16, int32, int64
uint8, uint16, uint32, uint64

int

NaN

float('nan')

Inf

float('inf')

logical

bool

char array (1-by-N or N-by-1)

str

string

str

string array (1-by-N or N-by-1)

list of str

struct

dict

cell array (1-by-N or N-by-1)

list

Unsupported MATLAB Data Types

The following conversions from MATLAB data types to Python data types are not supported.

  • char array (M-by-N)

  • string array (M-by-N)

  • cell array (M-by-N)

  • Sparse array

  • struct array

  • datetime

  • duration

  • dictionary

  • table

  • timetable

  • Objects not produced by MATLAB (such as Java® objects)

  • Function handle

  • MATLAB handle object (such as the containers.Map type)

  • MATLAB value object (such as the categorical type)

See Also

Classes

Topics