Pass MATLAB Data to Python - MATLAB & Simulink (original) (raw)
Main Content
When you call a Python® function in MATLAB®, the Python interface in MATLAB converts the 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 MATLAB Scalar Data to Python
In MATLAB, when you pass scalar data from MATLAB to a Python function, the Python interface converts the data into the equivalent Python data types.
Pass MATLAB Array Data to Python
In MATLAB, when you pass a MATLAB array as input to a Python function and the NumPy module is available in the Python environment, the Python interface automatically converts the array to a Python NumPy array. If the NumPy module is not available when you pass a MATLAB array as input to a Python function, the Python interface handles vector input as it does matrix input—the Python interface converts these inputs to Pythonmemoryview
objects. (since R2025a)
Before R2025a: When you pass a MATLAB vector with or without the NumPy package to a Python function, the Python interface converts the vector to a Python array.array
object.
MATLAB Input Array Type | Resulting Python Type with NumPy |
---|---|
double (real) | numpy.array(dtype=np.float64) |
single (real) | numpy.array(dtype=np.float32) |
int8 (real) | numpy.array(dtype=np.int8) |
uint8 (real) | numpy.array(dtype=np.uint8) |
int16 (real) | numpy.array(dtype=np.int16) |
uint16 (real) | numpy.array(dtype=np.uint16) |
int32 (real) | numpy.array(dtype=np.int32) |
uint32 (real) | numpy.array(dtype=np.uint32) |
int64 (real) | numpy.array(dtype=np.int64) |
uint64 (real) | numpy.array(dtype=np.uint64) |
double (complex) | numpy.array(dtype=np.complex128) |
single (complex) | numpy.array(dtype=np.complex64) |
logical | numpy.array(dtype=np.bool) |
char vector | str |
cell vector | tuple |
datetime array | numpy.datetime64 array |
duration array | numpy.timedelta64 array |
char matrix string array | This conversion is not supported. |
The Python language provides a protocol for accessing memory buffers like the data stored in a MATLAB array. If NumPy is not installed, the Python interface implements this Python buffer protocol for MATLAB arrays so that you can read MATLAB arrays directly from Python code, running in the same process as MATLAB, without copying data. This data buffer is then converted to a Pythonmemoryview
object. In this example, the Python interface converts the MATLAB array marr
to a memoryview
object while passing it to the array.array
constructor.
marr = [1,2,3]; pyarr = py.array.array('d',marr);
Unsupported MATLAB Types
These MATLAB types are not supported in Python.
- Multidimensional
char
orcell
arrays - Sparse arrays
struct
arrayscategorical
containers.Map
- MATLAB objects
matlab.metadata.Class
(py
.class
)