如何从 MATLAB 调用 Python 函数?
pythonserver side programmingprogramming更新于 2025/12/24 8:12:17
Python 库现在可在 MATLAB 中使用(自 2014b 起)。如果我们使用的是 2014b 或更高版本,我们可以直接在 MATLAB 中运行代码。
这使得在 MATLAB 中使用 Python 模块成为可能。无需任何其他更改,只需在要使用的 Python 库名称前加上前缀"py"。让我们以 Python 日历模块为例。
py.calendar.isleap(2016); py.calendar.isleap(2017);
输出
ans =1 ans = 0
要运行我们自己的函数,我们可以在当前 MATLAB 工作目录中创建一个文件。假设我们创建了一个名为"hello.py"的文件,其中包含以下两行:
def world(): return 'hello world';
# 在 MATLAB 中,如果我们运行以下命令,我们将获得以下输出
py.hello.world();
输出
Hello world!
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

