从 Matlab 调用 Python - 如何调试 Python 代码?

ivb*_*sd1 3 python debugging matlab

我正在研究主要用 Matlab 和部分用 Python 编写的系统。Python 脚本是从 Matlab 调用的。我正在寻找使用一些 IDE(如 PyCharm)调试从 Matlab 调用的 Python 代码的便捷方法。

如果可能以及如何获得建议,我会很高兴。Windows 10,Matlab R2018b,Python 3.6

Unb*_*ess 5

这是在 Microsoft Visual Studio 中调试从 MATLAB 执行的 Python 代码的解决方案。

此解决方案已在 Windows 10 上使用 Python 3.6、MATLAB R2020a 和 Visual Studio 2015 进行测试。


创建您的.py文件:

# mymod.py
"""Python module demonstrates passing MATLAB types to Python functions"""
def search(words):
    """Return list of words containing 'son'"""
    newlist = [w for w in words if 'son' in w]
    return newlist


def theend(words):
    """Append 'The End' to list of words"""
    words.append('The End')
    return words
Run Code Online (Sandbox Code Playgroud)

在 MATLAB 中,导入模块并创建一个虚拟列表:

>> mod = py.importlib.import_module('mymod');
>> N = py.list({'Jones','Johnson','James'});
Run Code Online (Sandbox Code Playgroud)

打开 Visual Studio 并从现有代码创建一个新的 Python 项目。然后,从调试菜单中选择附加到进程

在此处输入图片说明

搜索 MATLAB:

在此处输入图片说明

选择 MATLAB 进程并附加。

在你的代码中放置一个断点:

在此处输入图片说明

现在回到 MATLAB 并调用该函数:

>> py.mymod.search(N)
Run Code Online (Sandbox Code Playgroud)

MATLAB 命令窗口将停止。转到 Visual Studio 并调试您的代码:

在此处输入图片说明