python - 从 MATLAB 调用 Python 函数

标签 python matlab language-interoperability

我需要从 MATLAB 调用 Python 函数。我该怎么做?

最佳答案

我对我的系统有类似的要求,这是我的解决方案:

在 MATLAB 中有一个名为 perl.m 的函数,它允许您从 MATLAB 调用 perl 脚本。根据您使用的版本,它将位于类似的位置

C:\Program Files\MATLAB\R2008a\toolbox\matlab\general\perl.m

创建一个名为 python.m 的副本,用 python 快速搜索和替换 perl,仔细检查它设置的命令路径以指向你的 python 安装。您现在应该可以从 MATLAB 运行 python 脚本了。

示例

python 中保存为“sqd.py”的简单平方函数,如果我正确执行此操作,我自然会在测试输入参数、有效数字等方面进行一些检查。

import sys

def squared(x):
    y = x * x
    return y

if __name__ == '__main__':
    x = float(sys.argv[1])
    sys.stdout.write(str(squared(x)))

然后在 MATLAB 中

>> r=python('sqd.py','3.5')
r =
12.25
>> r=python('sqd.py','5')
r =
25.0
>>

关于python - 从 MATLAB 调用 Python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1707780/

相关文章:

python - IndexError : list index out of range , while finding the first non-consecutive number in a list in python

python - 找不到 Django==2.2 的匹配分布

python - racket程序与python程序的通信

ios - Swift 和 Objc 的互操作性——可用性不仅仅在 objc 中起作用

python - 存储评论数据 python 的最快方法

matlab - 分类(LDA)函数的作用是什么?

matlab - 如何将 2 列图例添加到 Matlab 图中?

matlab - 消息目录 Matlab :interpreter was not loaded

objective-c - 如何从 Swift 扩展访问 Objective-C 类的私有(private)成员?

c - Fortran `double precision` 与 C `double` 不同的任何平台?