Can we stop at particular number of iteration?

조회 수: 6 (최근 30일)
Husni
Husni 2012년 11월 16일
I have a loop. The number of iteration is very big. Usually it produce error at iteration >500. can I run the loop and stop a few step before error? If I use debug, I need to run almost 500 iteration manually and it is tiring .
Best regard
Ali
  댓글 수: 3
Husni
Husni 2012년 11월 16일
what do you mean? the structure is similar to this (sorry not to post the code since it is very long):
for k= 1:10.000
%statement1 --------------
%statement2
end
and I want to stop at k=500 as example
C.J. Harris
C.J. Harris 2012년 11월 16일
Why don't you just run your for loop 500 times instead of 10000?

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2012년 11월 16일
편집: Jan 2012년 11월 16일
Solution 1:
dbstop if error
Then Matlab stops, when an error occurs.
Solution 2:
for k = 1:5e5
if k == 500
disp('dummy line'); % <- set a breakpoint in this line
end
... your code
end
Instead of a breakpoint, the "keyboard" command would be sufficient also.
Of course the debugger has more powerful options, but this simple methods work in other programming languages also and are easy to remember.
  댓글 수: 1
Husni
Husni 2012년 11월 16일
Many thanks jan, It is what I want
Ali

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Lakshminarayana Reddy Tamatam
Lakshminarayana Reddy Tamatam 2018년 11월 20일
Use 'keyboard' command to debug.
if i = 500
keyboard
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by