Pass Data Between MATLAB and Python - MATLAB & Simulink (original) (raw)
Main Content
When you return MATLAB® data to Python®, MATLAB Engine API for Python converts the data into the equivalent Python data type. MATLAB engine also converts incoming Python data into MATLAB data types.
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 objectSeeMATLAB 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 orN-by-1) | str |
string | str |
string array (1-by-N orN-by-1) | list of str |
struct | dict |
cell array (1-by-N orN-by-1) | list |
datetime | datetime.datetime |
duration | datetime.timedelta |
datetime array | numpy.datetime64 arrayIf the NumPy module is not available in the Python environment, MATLAB converts the datetime array tomatlab.object. |
duration array | numpy.timedelta arrayIf the NumPy module is not available in the Python environment, MATLAB converts theduration array tomatlab.object. |
table timetable | pandas.DataFrameOnly one level of nesting in tables is supported. If the Python Pandas module is not available in the Python environment, MATLAB converts thetable or timetable to amatlab.object. |
dictionary | matlab.dictionary. |
MATLAB handle object (such as thecontainers.Map type) | matlab.object MATLAB returns a reference to amatlab.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, the MATLAB engine converts the data into the equivalent MATLAB data types.
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