12

When I debug long pieces of numerical code, often I want to see the function variable values if something happens, or in an specific iteration. Generally I do:

function banana(platano)

% long stuff here

for ii=1:123456789
     % tons of maths
   if ii==45612
      stophere=1;    % I put a break point in this line of code
   end
end

However, this requires me to write code in the function for debugging and it doesn't look nice. Is there a smarter way of doing this?

0

1 Answer 1

15

One of the ways is using Conditional Breakpoints. You can add them by right clicking on the number of the line and selecting the "Set conditional Breakpoints..." option.

Example:

enter image description here

As described in the comments of this answer, if you want to set it with the command line you can use

dbstop in filename at linenumber if condition 

As an example:

dbstop in banana at 6 if ii==454345433

note, that the at linenumber and if condition are optional.

More things

Another useful tool of the debugger is breaking the program if there has been an error, using dbstop if error, as show in this Q&A.

Thanks to @Dev-il for showing me this!

Sign up to request clarification or add additional context in comments.

4 Comments

I didn't know that at all. I've always manually placed in if statements and paused the code that way. BTW, it would be even better if you could show how to do this via command-line through the command prompt rather than using the editor. Is this possible?
@rayryeng dbstop in file if condition
@rayryeng dbstop in banana at 8 if ii==454345433
Ander, consider adding in how to do this via command-line to make your post complete. Some of us (like me) don't use MATLAB's editor.

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.