Python Data Types in MATLAB

Data conversion, pass data between MATLAB® and Python®

When you call a Python method or function, MATLAB converts the input arguments into types that best represent the data to the Python language.

MATLAB automatically converts most Python numeric data into MATLAB types. For other data types, use MATLAB functions to convert Python data types explicitly to MATLAB types.

Topics

Passing Data

List, Tuple, and Dictionary Types

Commands for creating list, tuple, and dict types.

Pass Data to Python

When calling a Python function, MATLAB converts MATLAB data into types that best represent the data to the Python language.

Handle Data Returned from Python

How MATLAB converts data returned from Python into MATLAB types.

Access Elements in Python Container Types

A Python container is typically a sequence type (list or tuple) or a mapping type (dict).

String Arguments

Pass MATLAB Character Vector to Python Method

This example shows how to display the contents of a folder using the Python os.listdir method.

Use Python str Type in MATLAB

This example shows how to use the Python® path separator character (;).

Index into Python String

This example shows how to display the first character of a Python® str variable.

Pass MATLAB Backslash Control Character

This example shows how to pass the backslash control character (\) as a Python® str type.

List Arguments

Create Python list Variable

This example shows how to create a list variable to pass to a Python® function.

Use Python list Type in MATLAB

This example shows how to display folders on your system using the MATLAB disp function on the Python sys.path variable.

Use Python List of Numeric Types in MATLAB

This example shows how to convert a Python® list of numeric values into a MATLAB® array of double.

Index into Python List

This example shows how to display the last element in a Python® list variable.

Use Python List as Values in for Loop

This example shows how to display elements of a Python® list.

Pass Cell Array as Python Sequence Type

This example shows how to pass a MATLAB cell array to a Python function.

Read Element of Nested list Type

This example shows how to access an element of a Python® list variable containing list elements.

Display Stepped Range of Python Elements

This example shows how to use an incremental step in indexing.

Tuple Arguments

Create Python tuple Variable

This example shows how to create a tuple variable to pass to a Python® function.

Index into Python Tuple

This example shows how to display elements in a tuple.

Create Singleton Python tuple Variable

This example shows how to create a Python® tuple variable with a single element.

Dictionary (dict) Arguments

Create Python dict Variable

This example shows how to create a dict variable to pass to a Python® function.

Use Python dict Type in MATLAB

This example shows how to convert numerical values of a Python dict variable into a MATLAB array.

Index into Python dict

This example shows how to get and set values in a dictionary.

Pass dict Argument to Python Method

This example shows how to change a value in a dict variable using the Python® update method.

Convert Python dict Type to MATLAB Structure

This example shows how to plot numeric data from a Python dictionary.

Numeric Arguments

Default Numeric Types

By default, a number in MATLAB is a double type, but a number in Python is an integer type.

Use Python Numeric Types in MATLAB

This example shows how to use Python® numbers in MATLAB®.

Call Python Methods with Numeric Arguments

This example shows how to call the Python math.fsum function, which sums the floating-point values in an iterable input argument.

Use Python array Types in MATLAB

This example shows how to sum the elements of a Python array.array of type double.

View Python Numeric Values

This topic describes how MATLAB displays Python numeric values.

Keyword Arguments

Pass Keyword Arguments

This example shows how to use Python keyword arguments in MATLAB.

Function Arguments

Pass Python Function to Python map Function

This example shows how to display the length of each word in a list.