Q&A
Ask and answer questions to make information more available to wider audiences.
Madeline Lawrence @lawrencemadeline   22, Aug 2023 12:00 AM
break a loop iteration
How can you break a loop iteration, if it takes a long time?
answers 2
 
Answer 1
Chance Boorman @boormanchance   31, Aug 2023 10:42 AM
In case of placing multiple breakpoints it is vital to identify them e.g. by printing breakpoint info. It is crucial to design breakpoints in a way allowing to resume computations from the point it was disrupted. Sometimes instead of a command break it is better to put a message and a debugger breakpoint which will give you command-line access while the program is paused. Then you can continue from this exact point in code.
 
Answer 2
Chance Boorman @boormanchance   31, Aug 2023 10:41 AM
One way is performing a time control with the tic and toc functions. For example, if code takes an hour, then break the loop:
% for loop
time0 = tic;
timeLimit = 60*60*1; % 1 hour == 3600 seconds
  for ...
    ...
    if toc(time0)>timeLimit
      break
    end
  end


or using while loop:
time0 = tic;
timeLimit = 60*60*1; % 1 hour == 3600 seconds
  while conds && toc(time0)<timeLimit
    ...
  end