Main Content

Handle Data Returned from Python Function

Automatically Convert Python Types to MATLAB Types

MATLAB® automatically converts these data types returned from Python® functions into MATLAB types. To convert other types, see Explicitly Convert Python Types to MATLAB Types.

Python Return Type, as Displayed in Python

Resulting MATLAB Type — Scalar

float

double

complex

Complex double

int You must convert explicitly. See Explicitly Convert Python Types to MATLAB Types.

int64

bool

logical

datetime

datetime

All other Python types — type

Python object — py.type

Explicitly Convert Python Types to MATLAB Types

If the output of a Python function implements the Python buffer protocol, such as numpy.ndarray, and it is numeric or logical, 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.

Use these MATLAB functions to convert Python data types to MATLAB types.

Python Return Type or Protocol, as Displayed in MATLAB

MATLAB Conversion Function

Examples

py.str

string
char

Use Python str Variables in MATLAB

Object with __str__ method

char

py.help('datetime.date.__str__')
Help on wrapper_descriptor in datetime.date:

datetime.date.__str__ = __str__(self, /)
    Return str(self).
d = py.datetime.date(...
    int32(2020),int32(3),int32(4));
char(d)
ans = '2020-3-04'

py.int
py.long
py.float
py.bool

Numeric functions:
double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64

 
logical

py.bytes

uint8

 

py.array.array
py.numpy.ndarray
py.memoryview

You can convert py.array.array of any format and py.memoryview objects to the MATLAB type you want.

Numeric functions:
double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64

Use Python Numeric Variables in MATLAB, for example, Use Python Integer array Types in MATLAB.

py.list and py.tuple

double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64
logical
string
cell

Use Python list Variables in MATLAB
Use Python tuple Variables in MATLAB

For more information, see Error Converting Elements of list or tuple.

Mapping protocol; for example, py.dict

struct

Use Python dict Variables in MATLAB

py.datetime.datetime

datetime

dt = py.numpy.arange('2022-12-30', ...
    '2023-01-10',dtype='datetime64[D]');

py.datetime.timedelta

duration

td = py.numpy.array([1,2,3,4], ...
    dtype='timedelta64[h]');

For example, 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.

You can convert it to a MATLAB matrix P by typing:

P = double(p)
P = 3×3    
     8     1     6
     3     5     7
     4     9     2

If you need specific information about the Python properties of p, type:

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, Superclasses

If the Python module provides content in its __doc__ attribute, then MATLAB links to that information.

Related Examples

More About