从 MATLAB 中调用 Python
直接从 MATLAB® 调用 Python® 功能
您可以通过将 py. 前缀添加到 Python 名称,直接从 MATLAB 访问 Python 库。请参阅 从 MATLAB 访问 Python 模块。例如:
py.list({'This','is a','list'}) % Call built-in function list
py.textwrap.wrap('This is a string') % Call wrap function in module textwrappyrun 或 pyrunfile 函数直接在 MATLAB 中执行在 Python 解释器中执行的 Python 语句。例如:pyrun("l = ['A','new','list']") % Call list in Python interpreter反过来,如果您想要从 Python 应用程序调用 MATLAB 函数,请参阅从 Python 中调用 MATLAB以获取详细信息。
函数
实时编辑器任务
| 运行 Python 代码 | 在实时编辑器中运行 Python 语句或脚本文件 (自 R2024a 起) |
主题
在 MATLAB 中使用 Python 环境
- Manage Python Environments Using External Languages Panel
You can use the Python view in the External Languages panel to configure and maintain your Python environments directly from MATLAB.
在 MATLAB 中使用 Python 库
- 从 MATLAB 访问 Python 模块
在 MATLAB 中创建和使用 Python 对象。 - 配置您的系统以使用 Python
验证以确保您安装了受支持的 Python 版本。 - 调用用户定义的 Python 模块
创建本文档中的示例使用的一个 Python 模块。 - Understand Python Function Arguments
Python method syntax differs from MATLAB method syntax. - 高级主题
了解 MATLAB 和 Python 之间的代码模式差异。 - Python 功能的进程外执行
在独立于 MATLAB 进程的进程中执行 Python 脚本。 - 重新加载进程外 Python 解释器
重新加载进程外 Python 解释器而不重新启动 MATLAB。
从 MATLAB 运行 Python 代码
- 直接从 MATLAB 调用 Python 功能
从 Python 库中调用功能,或直接从 MATLAB 中执行 Python 语句。
传递数据
- 从 MATLAB 在 MATLAB 与 Python 之间传递数据
Python 接口将 MATLAB 数据转换为兼容的 Python 数据类型。 - Access Elements in Python Container Types
A Python container is typically a sequence type (listortuple) or a mapping type (dict). - Use Python Datetime Types in MATLAB
MATLAB supports conversions between MATLABdatetimevalues and Pythondatetimeor NumPydatetime64values. - Use Python Duration Types in MATLAB
MATLAB supports conversions between MATLABdurationvalues and Pythontimedeltaor NumPytimedelta64values. - Pass Python Function to Python map Function
This example shows how to display the length of each word in a list.
疑难解答
Determine If Error is Python or MATLAB Error
Determine if an error originates in Python or MATLAB code.
对加载 Python 失败进行故障排除。
Python 中不支持的 MATLAB 功能。
MATLAB catches exceptions thrown by Python and converts them into a matlab.exception.PyException object.
Troubleshooting Matrix and Numeric Argument Errors
Errors might be caused by an input array with more than one dimension whose size is greater than 1.
Error Converting Elements of list or tuple
Use string and numeric converters for list and
tuple types.
精选示例
教学资源
编程:结合使用 MATLAB 和 Python 的入门工程
了解如何调用 Python 代码、转换数据类型和单位,以及使用 App 设计工具构建 App 来显示结果。

