本页对应的英文页面已更新,但尚未翻译。 若要查看最新内容,请点击此处访问英文页面。

MATLAB 中的 Python 数据类型

数据转换、在 MATLAB® 和 Python® 之间传递数据

当您调用 Python 方法或函数时,MATLAB 会将输入参数转换为最适合表示 Python 语言的数据的类型。

MATLAB 会自动将大多数 Python 数值数据转换为 MATLAB 类型。对于其他数据类型,请使用 MATLAB 函数将 Python 数据类型显式转换为 MATLAB 类型。

主题

传递数据

列表、元组和字典类型

用于创建 listtupledict 类型的命令。

将数据传递给 Python

当调用 Python 函数时,MATLAB 将 MATLAB 数据转换为最适合在 Python 语言中表达该数据的类型。

处理从 Python 返回的数据

MATLAB 如何将从 Python 返回的数据转换为 MATLAB 类型。

Access Elements in Python Container Types

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

字符串参数

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.

列表参数

Create Python list Variable

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

在 MATLAB 中使用 Python list 类型

此示例说明如何对 Python sys.path 变量使用 MATLAB disp 函数以显示您系统上的文件夹。sys.pathlist 类型。

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.

元组参数

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.

字典 (dict) 参数

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.

数值参数

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.

关键字参数

Pass Keyword Arguments

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

函数参数

Pass Python Function to Python map Function

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