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!