Main Content

使用权Python模块fromMATLAB- 入门

您可以访问所有标准的Python®MATLAB的库内容®。同样,您可以在第三方或用户创建的模块中使用功能。要直接从MATLAB调用Python功能,请添加py。您要调用的Python函数名称的前缀。

  • 要在Python标准库中调用内容,请添加py。在Python函数或类名称的前面。

    py.list({'这个',,,,'是一个,,,,'列表'})% Call built-in function list
  • To call content in available modules, addpy。在Python模块名称的前面,然后是Python函数或类名称。

    py.textwrap.wrap('这是一个字符串'%呼叫包裹功能在模块文本处理中

You do not need to import modules in order to use them. However, you may import Python names into your MATLAB function in the same way that you can import content in MATLAB packages. For more information, see了解Python和Matlab导入命令

MATLAB还提供了一种直接从Matlab中直接从Python解释器中运行Python代码的方法。有关更多信息,请参阅Directly Call Python Functionality from MATLAB

学习目标

本教程解释了如何:

  • 检查计算机上的Python版本。

  • Create a Python object and call a method on it.

  • 显示Python模块的帮助。

  • Create specialized Python列表,,,,tuple,,,,anddict(dictionary) types

  • Call a method on a Python object with the same name as a MATLAB function.

  • 从您自己的Python模块调用功能。

  • 查找示例。

核实Python配置

要在MATLAB中使用Python,您必须在计算机上安装Python的支持版本。金宝app要验证您有一个受支持的版本,请键入:金宝app

Pyenv
ans =带有属性的Pythonenvironment:版本:“ 3.8”可执行文件:“ C:\ Users \aname\AppData\Local\Programs\Python\Python38\pythonw.exe" Library: "C:\Users\aname\ appdata \ local \ program \ python \ python38 \ python38.dll“ home:” c:\ users \aname\AppData\Local\Programs\Python\Python38" Status: NotLoaded ExecutionMode: OutOfProcess

If the value of the版本属性为空,然后您没有支持的版本。金宝app有关安装Python的更多信息,请参阅配置您的系统以使用Python

使用权Python标准库模块MATLAB

MATLAB与计算机上的Python解释器进行交互,使您可以访问所有标准库内容。例如,创建一个python列表数据类型。

res= py.list({'Name1',,,,'name2',,,,'name3'})
res =没有属性的Python列表。['name1','name2','name3']

MATLAB识别Python对象,并自动将MATLAB单元格数组转换为适当的Python类型。

您可以在对象上调用Python方法。显示可用方法列表objects, type方法(py.list)。For example, update the listres使用Python附加功能。

res。附加('name4'
res =没有属性的Python列表。['name1','name2','name3','name4']

To convert the列表变量到MATLAB变量,调用cell在列表中,char在列表的元素上。

myList = cellfun(@char,cell(res),“统一输出”,,,,false)
myList = 1×4个小区阵列{'name1'} {'name2'} {'name3'} {'name4'}

展示Python文档inMATLAB

You can display help text for Python functions in MATLAB. For example:

py。help(“list.append”
在list:list.append = append(...)l.append(object) - > none-附录对象结束

键入时选项卡完成py。不显示可用的Python功能。有关更多信息,请参阅Help for Python Functions

Create List, Tuple, and Dictionary Types

该表显示了用于创建的语句列表,,,,tuple,,,,anddicttypes. The statements on the left are run from the Python interpreter. The statements on the right are MATLAB statements.

Python列表-[]

MATLABpy.list

>>> ['Robert','Mary','Joseph'] >> py.list({'robert','玛丽','joseph'})
>>> [[1,2],[3,4]] >> py.list({py.list([1,2]),py.list([3,4])})

Pythontuple-()

MATLABpy。tuple

>>>(“罗伯特”,19,“生物学”) >> py.tuple({'robert',19,'Biology'})

Pythondict-{}

MATLABpy。dict

> > >{的罗伯特': 357, 'Joe': 391, 'Mary': 229} >> py.dict(pyargs(...
“罗伯特”,357,'玛丽',229,'乔',391))


有关通过关键字参数的信息,请参阅pyargs

Precedence Order of Methods and Functions

如果Python类定义了具有与Python类型的MATLAB转换器方法相同名称的方法,则MATLAB称为Python方法。这意味着您无法在该类的对象上调用MATLAB转换器方法。

For example, if a Python class defines achar方法,此语句称为Python方法。

char(obj)

使用MATLABchar功能,类型:

char(py.str(obj))

使用权OtherPython模块

You can use your own Python code and third-party modules in MATLAB. The content must be on the Python path. Installing a third-party module puts the content on the Python path. If you create your own modules, you are responsible for putting them on the path.

For an example, seeCall User-Defined Python Module

Python例子

例如,您可以在MATLAB LIVE编辑器中打开代码,在此处查找特色示例Call Python from MATLABpage. For information about searching MATLAB examples, seeMATLABCode Examples

有关使用在线数据集的示例,请参阅此信息Mathworks博客文章

也可以看看

Related Topics