Main Content

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 —
Scalar Values Only

Resulting Python py. Type

Examples

double (real)
single (real)

float (default) or int

Use Python Numeric Variables in MATLAB

By default, the Python interface converts type double or single to Python float.
For integer values based on Python type hints, the Python interface can convert type double or single to Python int.

double (complex)
single (complex)

complex

z = complex(1,2);
py.cmath.polar(z)
ans = 
  Python tuple with values:

    (2.23606797749979, 1.1071487177940904)

int8
uint8
int16
uint16
int32

int

-

uint32
int64
uint64

int

-

NaN

float("nan")

-

Inf

float("inf")

-

string scalar
char vector

str

Use Python str Variables in MATLAB

<missing> value in string

None

py.list({string(missing),"Value"})
ans = 
  Python list with values:

    [None, 'Value']

logical

bool

-

dictionary

dict

Use Python Dictionaries in MATLAB

struct

dict

Use Python Dictionaries in MATLAB
tablepy.pandas.DataFrameUse Python Pandas DataFrames in MATLAB
timetablepy.pandas.DataFrameUse Python Pandas DataFrames in MATLAB
datetime

py.datetime.datetime

Use MATLAB Datetime Types with Python
duration

py.datetime.timedelta

Use MATLAB Duration Types with Python

Python object — py.type

type

-

function handle @py.module.function, to Python functions only

module.function

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

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 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 or cell arrays

  • Sparse arrays

  • struct arrays

  • categorical

  • containers.Map

  • MATLAB objects

  • matlab.metadata.Class (py.class)

See Also

Topics