Pass MATLAB Data to Python
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.
MATLAB Input Argument Type — | Resulting Python
| Examples |
---|---|---|
|
| Use Python Numeric Variables in MATLAB |
|
|
z = complex(1,2); py.cmath.polar(z) ans = Python tuple with values: (2.23606797749979, 1.1071487177940904) |
|
| - |
|
| - |
|
| - |
|
| - |
|
| Use Python str Variables in MATLAB |
|
|
py.list({string(missing),"Value"}) ans = Python list with values: [None, 'Value'] |
|
| - |
|
| Use Python Dictionaries in MATLAB |
|
| Use Python Dictionaries in MATLAB |
table | py.pandas.DataFrame | Use Python Pandas DataFrames in MATLAB |
timetable | py.pandas.DataFrame | Use Python Pandas DataFrames in MATLAB |
datetime |
| Use MATLAB Datetime Types with Python |
duration |
| Use MATLAB Duration Types with Python |
Python object —
|
| - |
function handle
|
| Pass Python Function to Python map Function |
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 Python
memoryview
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 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Python
memoryview
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
arraysSparse arrays
struct
arrayscategorical
containers.Map
MATLAB objects
matlab.metadata.Class
(py
.class
)