简体   繁体   English

如何从Python调用MATLAB脚本时调试它

[英]How to debug MATLAB script when calling it from Python

I'm calling a MATLAB script from python using 我正在使用python调用MATLAB脚本

ml = matlab.engine.start_matlab()
output = ml.myMATLABscript(input1, input2)

The script runs fine in MATLAB, but when I call it from Python it runs into int vs double issues. 该脚本在MATLAB中运行良好,但是当我从Python调用它时会遇到int vs double问题。 So far I've been fixing this by interpreting the error message when the script crashes, but it would be nice to see what's going on specifically. 到目前为止,我一直在通过解释脚本崩溃时的错误消息来解决这个问题,但是看看具体发生了什么会很好。 For this purpose, I'd like to be able to step through the MATLAB code line for line, even though I've called it from Python. 为此,我希望能够逐步完成MATLAB代码行,即使我已经从Python调用它。

Turns out this is easier than I expected. 事实证明这比我预期的要容易。 Simply programatically set a breakpoint in the MATLAB script. 只需在MATLAB脚本中以编程方式设置断点即可。 For example: 例如:

dbstop if error

Then call the script from Python as before. 然后像以前一样从Python调用脚本。 The MATLAB editor will open in debug mode at the specified breakpoint. MATLAB编辑器将在指定的断点处以调试模式打开。

This is also possible without editting the MATLAB script. 这也可以在不编辑MATLAB脚本的情况下实现。 In that case you need to set the MATLAB breakpoint from Python, using the enginge's eval: 在这种情况下,您需要使用enginge的eval从Python设置MATLAB断点:

ml = matlab.engine.start_matlab()
ml.eval(dbstop in myMATLABscript if error)
output = ml.myMATLABscript(input1, input2)

For completeness, from MATLAB's documentation : 为了完整起见 ,请参阅MATLAB的文档

  • dbstop in file sets a breakpoint at the first executable line in file. dbstop in file的第一个可执行行设置断点。
  • dbstop in file at location sets a breakpoint at the specified location dbstop in file at location指定位置设置断点
  • dbstop in file if expression sets a conditional breakpoint at the first executable line of the file dbstop in file if expression的第一个可执行行设置条件断点,则dbstop in file if expression file中
  • dbstop in file at location if expression sets a conditional breakpoint at the specified location. dbstop in file at location if expression在指定位置设置条件断点,则dbstop in file at location if expression

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM