Main Content

Pass Data Between MATLAB and Python

When calling a Python® function, MATLAB® Compiler SDK™ converts MATLAB data into types that best represent the data in the Python language. For information about using Python data in MATLAB, see Handle Data Returned from Python Function.

Pass Data from MATLAB to Python

When you pass data from MATLAB to Python, either directly or as output from a MATLAB function in 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

datetime

datetime.datetime

duration

datetime.timedelta

datetime array

numpy.datetime64 array
If the NumPy module is not available in the Python environment, MATLAB converts the datetime array to matlab.object.

duration array

numpy.timedelta array
If the NumPy module is not available in the Python environment, MATLAB converts the duration array to matlab.object.

table
timetable

pandas.DataFrame
Only one level of nesting in tables is supported. If the Python Pandas module is not available in the Python environment, MATLAB converts the table or timetable to a matlab.object.
For more information, see Convert Between MATLAB tables and Pandas DataFrames.

dictionary

matlab.dictionary.

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

matlab.object
MATLAB returns a reference to a matlab.object, not the object itself. You cannot pass a matlab.object between MATLAB sessions.

MATLAB value object (such as the categorical type)

matlab.object
You can pass a value object to a MATLAB function, but you cannot create or modify it.

Unsupported MATLAB Data Types

Along with the exceptions noted in the table, MATLAB does not convert these MATLAB data types to Python data types:

  • char array (M-by-N)

  • string array (M-by-N)

  • cell array (M-by-N)

  • Sparse array

  • struct array

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

  • Function handle

Pass Data from Python to MATLAB

When you pass data from Python to MATLAB, either directly or as input arguments to a MATLAB function in Python, MATLAB 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 Python Module.

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

Numeric array

float

double

complex

Complex double

int

int64

float('nan')

NaN

float('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 Python Dictionaries in MATLAB.

datetime.datetime
numpy.datetime64 scalar

datetime

datetime.timedelta
numpy.timedelta64 scalar

duration

numpy.datetime64 array

datetime array

numpy.timedelta64 array

duration array

pandas.DataFrame

table
Only one level of nesting in tables is supported.
For more information, see Convert Between MATLAB tables and Pandas DataFrames.

pandas.DataFrame with datetime column

timetable
Only one level of nesting in timetables is supported.
For more information, see Convert Between MATLAB tables and Pandas DataFrames.

matlab.dictionary

dictionary
matlab.dictionary keys and values must meet certain specifications. MATLAB converts the data in a matlab.dictionary object using the mappings in this table, with the exception that str data is converted to string. See Use Python Dictionaries in MATLAB.

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

dictionary

Unsupported Python Data Types

Along with the exceptions noted in the table, MATLAB does not convert these Python data types to MATLAB data types:

  • Python class (module.type) objects

  • None object

See Also

Topics