1

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.

1 Answer 1

4

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Very nice, just out of curiosity: Does this need to go in the actual matlab script, or can you call it seperately from python (so you don't need to edit the script to debug).
Yes actually! I'll expand the answer to specify how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.