2

I need your help. I want debug a matlab code after specific iteration. Suppose the following is the code:

**im=imread('C:\lena.tif');   
[m n]=size(im); 

for i=1:2:m-1  
    for j=1:2:n-1  
        enter into a function  
        ..................   
        ..................**

Suppose, when i = 505 and j = 460, the program will enter into the Debug Mode, and then I will debug the rest of the code using STEP IN (F11)

Please help me. Please please please...... Many thanks in advance.

Regards - Jessy

4
  • 5
    See: Conditional Breakpoints. Or just make a dummy if i == 505 && j == 460 statement. Commented Jan 12, 2016 at 4:16
  • 4
    Here is a Q&A on using conditional breakpoints: stackoverflow.com/questions/34027454/… Hope it helps you. Commented Jan 12, 2016 at 7:44
  • @mikkola it is even a dupe I think. Commented Jan 12, 2016 at 8:11
  • @patrik Yeah, also noticed that and flagged. Commented Jan 12, 2016 at 9:07

1 Answer 1

3

As others have stated in the comments, you can use conditional breakpoints. It is important to point out that this can be done both interactively through the MATLAB editor or programmatically

dbstop in FUNCTION_NAME at LINE_NUMBER if CONDITION

Furthermore, you can also combine conditionals within your code and the use of keyboard to be able to interact with the code at a specific location.

for i=1:2:m-1  
    for j=1:2:n-1
         if i == 505 && j == 460
             keyboard % Now you can step through the "do stuff" below
         end

         % do stuff
    end
end

As a side note, a REALLY useful debugging tip is to actually set dbstop to be triggered when ANY error is thrown.

dbstop if error

This will set a breakpoint right where any failure occurs. You can then use all the editor debugging tools or debugging commands (dbup, dbdown, dbstep, etc.) to go to where the real issue lies and look at the current state.

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

1 Comment

Hi Everyone, Thank you so much for your wise and helpful answer. I am very grateful to you people. Thanks again.

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.