Handle Data Returned from Python

Automatic Python Type to MATLAB Type Mapping

The following table shows how MATLAB® converts data returned from Python® into MATLAB types.

Python Return Type, as Displayed in Python

Resulting MATLAB Type — Scalar

bool

logical

int (version 2.7 only). For Python versions 3.x int, you must convert explicitly.

int64

float

double

complex

Complex double

All other Python types — type

Python object — py.type

Explicit Type Conversions

MATLAB provides the following functions to convert Python data types to MATLAB types manually.

Python Return Type or Protocol, as Displayed in MATLAB

MATLAB Conversion Function

py.str (version 2.7)

string
char
uint8

py.str (version 3.x)

string
char

py.unicode

string
char

Object with __str__ method

char

py.bytes

uint8

py.int

double
or
int64

py.long

double
or
int64

py.array.array[a]

numeric
double
single
int8
uint8
int16
uint16
int32
uint32
int64
uint64

Sequence protocol; for example, py.list and py.tuple

cell

Mapping protocol; for example, py.dict

struct

[a] You can convert py.array.array of any format to the MATLAB type you want.

Related Topics