How to utilise the MATLAB debugger mode to initialise a random iteration in a FOR loop ?
10 次查看(过去 30 天)
显示 更早的评论
Hi there,
Just imagine that I have a for loop in a code as show below.
for count = 1 : 100
do something
end
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 ?
0 个评论
采纳的回答
Walter Roberson
2020-5-10
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!