Clear Filters
Clear Filters

How to utilise the MATLAB debugger mode to initialise a random iteration in a FOR loop ?

21 views (last 30 days)
Hi there,
Just imagine that I have a for loop in a code as show below.
for count = 1 : 100
do something
I have found that there is something wrong with my code (a logical error) at the 31st iteration.
So If I am using the debugger mode in MATLAB, I should add a breakpoint at the beginning and the end of the for loop to go through each step and see what is happening inside my code. The problem I have there is, should I always start iterating the for loop from
count =1
Instead can I start with count = 27 or somewhere closer so I it would easier my debugging ?

Accepted Answer

Walter Roberson
Walter Roberson on 10 May 2020
If the iterations do not affect each other, then you can put a breakpoint at the "do something" line, execute, and when it stops, assign the desired value to count. That will only affect until the end of that one iteration, and the next loop iteration through, count will become 2 no matter what you set it to. But you can then manually set to 2, and keep doing that.
But what you probably need is to set a conditional breakpoint. Right-click the grey bubble to the left of the line, and choose Set Conditional Breakpoint. In the window that comes up, enter
count >= 27
and press return. Now execute the code. It will run the first 26 iterations and then stop when it gets to iteration 27.

More Answers (0)


Find more on Loops and Conditional Statements in Help Center and File Exchange





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!