5Ke
5Ke

Reputation: 1289

How to debug MATLAB script when calling it from Python

I'm calling a MATLAB script from python using

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. 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.

Upvotes: 1

Views: 1235

Answers (1)

5Ke
5Ke

Reputation: 1289

Turns out this is easier than I expected. Simply programatically set a breakpoint in the MATLAB script. For example:

dbstop if error

Then call the script from Python as before. The MATLAB editor will open in debug mode at the specified breakpoint.

This is also possible without editting the MATLAB script. In that case you need to set the MATLAB breakpoint from Python, using the enginge's eval:

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

For completeness, from MATLAB's documentation:

  • dbstop in file sets a breakpoint at the first executable line in file.
  • dbstop in file at location sets a breakpoint at the specified location
  • dbstop in file if expression sets a conditional breakpoint at the first executable line of the file
  • dbstop in file at location if expression sets a conditional breakpoint at the specified location.

Upvotes: 4

Related Questions