我已按照 this 上的说明进行操作页面让 MATLAB 在 python 模块中调用 python 代码。
# mymod.py
def search(words):
"""Return list of words containing 'son'"""
newlist = [w for w in words if 'son' in w]
return newlist
然后在 MATLAB 中运行
N = py.list({'Jones','Johnson','James'})
py.mymod.search(N)
并获得所需的输出。但是,当 python 模块本身有导入时,我无法让事情正常工作。例如,将单行 import numpy as np
添加到 mymod.py
会产生错误
Undefined variable "py" or class "py.mymod.search"
在 MATLAB 中运行相同的代码行。我该如何解决这个问题?
最佳答案
在 matlab在本书第 110 页上,您可以看到导致您看到此错误的可能原因列表以及如何解决这些问题。
原因:
- “未安装 Python”(第 1-70 页)
- 第 1-70 页上的“Windows 平台上的 Python 64 位/32 位版本”
- 第 1-71 页上的“MATLAB 找不到 Python”
- “用户定义的 Python 模块中的错误”第 1-71 页
- “Python 模块不在 Python 搜索路径上”第 1-71 页
- 第 1-72 页的“模块名称冲突”
- “Python 尝试在错误的模块中执行命令”第 1-72 页
我建议您完成此解决方案列表,因为您可以看到导致错误的可能原因有多种,如果没有更多信息,我们很难知 Prop 体是哪一个。
关于python - MATLAB 无法调用带有导入的 Python 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45848273/