Pass Data Between MATLAB and Python from MATLAB
When you call a Python® function in MATLAB®, the Python interface converts the MATLAB input data into types that best represent the data in the Python language. The Python interface also converts some returned Python data into MATLAB data types.
Pass Data from MATLAB to Python
When you pass data from MATLAB to Python, the resulting Python type depends on whether the MATLAB data is a scalar value or an array.
Pass Scalar Value from MATLAB to Python
In MATLAB, when you pass a scalar value as input to a Python function, the Python interface automatically converts the data into the equivalent Python data type.
MATLAB Input Type | Resulting Python Data Type | Examples |
|---|---|---|
|
| Use Python Numeric Variables in MATLAB |
|
|
z = complex(1,2); py.cmath.polar(z) ans =
Python tuple with values:
(2.23606797749979, 1.1071487177940904)
|
|
| |
|
|
n = NaN; py.float(n) ans =
Python float with properties:
imag: 0
real: NaN
nan |
|
|
i = Inf; py.float(i) ans =
Python float with properties:
imag: 0
real: Inf
inf
|
|
| Use Python str Variables in MATLAB |
| " |
s = string(missing); py.str(s) ans =
Python str with no properties.
None
|
|
|
b = true; py.type(b) ans =
Python type with no properties.
<class 'bool'> |
|
| Use Python Dictionaries in MATLAB |
| pandas.DataFrame | Use Python Pandas DataFrames in MATLAB |
datetime |
| Use Python Datetime Types in MATLAB |
duration |
| Use Python Duration Types in MATLAB |
Python object —
|
| - |
function handle
|
| Pass Python Function to Python map Function |
Pass Array Data from MATLAB 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 (for Array Data Only) | Resulting Python Data Type with NumPy |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1-by- |
|
|
|
|
|
| No automatic conversion. |
If NumPy is not installed and a numeric MATLAB array is passed to Python, the Python interface creates a memoryview object that points to the
array contents. This memoryview object implements the Python buffer protocol and does not make a copy of the array. In this example, an
array.array is constructed from the memoryview
object created from the MATLAB array.
mArr = [1,2,3];
pyArr = py.array.array('d',mArr);
Explicitly Convert MATLAB Types to Python Types
While the Python interface does not automatically convert multidimensional MATLAB string data, you can explicitly convert the data to a Python data type.
MATLAB Type | Resulting Python Type | MATLAB Conversion Function |
|---|---|---|
string array | NumPy |
Unsupported MATLAB Types
These MATLAB types are not supported in Python:
Sparse arrays
Nonscalar
structarrayscategoricalcontainers.MapMATLAB objects
matlab.metadata.Class(py.class)
Pass Data from Python to MATLAB
When you call Python functions from within MATLAB, the functions can return data of any data type. Depending on the data type returned by the Python function, the Python interface might automatically convert the output into a MATLAB data type. If the Python interface does not automatically convert the data, the Python function returns output of a Python data type, which you can then manually convert into a MATLAB data type.
Return Scalar Value from Python to MATLAB
When a Python function returns a scalar value of any of these Python data types, the Python interface automatically converts the value into these MATLAB types.
Python Return Type, as Displayed in Python | Resulting MATLAB Data Type |
|---|---|
|
|
| Complex |
|
|
datetime |
|
| string |
Explicitly Convert Python Types to MATLAB Types
When a Python function returns any other type of data, the Python interface does not automatically convert the data. You can explicitly convert Python data types to MATLAB types using these MATLAB functions.
Python Return Type or Protocol, as Displayed in MATLAB | MATLAB Conversion Function | Examples |
|---|---|---|
|
| Use Python str Variables in MATLAB |
Object with |
|
py.help('datetime.date.__str__')Help on wrapper_descriptor in datetime.date:
datetime.date.__str__ = __str__(self, /) unbound datetime.date method
Return str(self).d = py.datetime.date(...
int32(2020),int32(3),int32(4));
char(d)ans = '2020-3-04' |
|
| Use Python Numeric Variables in MATLAB |
py.bool |
|
x = py.bool(true); double(x) ans =
1
|
|
|
b = py.bytes("MATLAB",'utf-8'); uint8(b) ans = 1×6 uint8 row vector 77 65 84 76 65 66 |
|
| Use Python Numeric Variables in MATLAB |
|
| |
|
| Use Python list Variables in MATLAB |
|
| Use Python Dictionaries in MATLAB |
py.pandas.DataFrame |
| Use Python Pandas DataFrames in MATLAB |
|
| Use Python Datetime Types in MATLAB |
|
| Use Python Duration Types in MATLAB |
Unsupported Python Types
The Python
enum type is not supported in MATLAB.
Convert Python ndarray to MATLAB Array
If the output of a Python function has a type, such as numpy.ndarray, that implements
the Python buffer protocol, then MATLAB displays:
The actual Python type.
The underlying data.
The corresponding MATLAB conversion function. Use this function to fully convert the Python object to a MATLAB array.
For example, suppose a Python function returns this array p:
p =
Python ndarray:
8 1 6
3 5 7
4 9 2
Use details function to view the properties of the Python object.
Use double function to convert to a MATLAB array.Convert p to a MATLAB matrix M by using the suggested conversion function.
M = double(p)
M = 3×3
8 1 6
3 5 7
4 9 2
Get specific information about the Python properties of p.
details(p)
py.numpy.ndarray handle with properties:
T: [1×1 py.numpy.ndarray]
base: [1×1 py.NoneType]
ctypes: [1×1 py.numpy.core._internal._ctypes]
data: [1×3 py.memoryview]
dtype: [1×1 py.numpy.dtype[float64]]
flags: [1×1 py.numpy.flagsobj]
flat: [1×1 py.numpy.flatiter]
imag: [1×1 py.numpy.ndarray]
itemsize: [1×1 py.int]
nbytes: [1×1 py.int]
ndim: [1×1 py.int]
real: [1×1 py.numpy.ndarray]
shape: [1×2 py.tuple]
size: [1×1 py.int]
strides: [1×2 py.tuple]
Methods, Events, SuperclassesIf the Python module provides content in its __doc__ attribute, then
MATLAB links to that information.