필터 지우기
필터 지우기

Is there a way for breaking a loop iteration, if it takes to long?

조회 수: 37 (최근 30일)
Philipp
Philipp 2016년 6월 7일
댓글: Gauri Kant 2021년 6월 18일
Dear People, I am running analyzes of multiple input data files. Each file is analyzed in one iteration of a for loop, e.g.
for 1:length(data)
analyze(data{i});
end
However, for some data, the analysis takes very long, because the RAM is full. Is there a way of breaking the loop on a timer or RAM condition, say after 1 hour of iteration time, or as RAM gets full? Or do you have any other recommendation, except for throughing the data away in advance, or tuning the analysis?
Kind regards, Philipp

답변 (2개)

Jan Orwat
Jan Orwat 2016년 6월 7일
One of the simplest ways for time control is to use tic and toc
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
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 command break it is better to put a message and a debbuger breakpoint which will give you command line access while program is paused. Then you can continue from this exact point in code.
  댓글 수: 6
Walter Roberson
Walter Roberson 2021년 6월 17일
Use the Parallel Computing Toolbox.
Use parfeval() to start the computation of an interation. This will return a "future", which is a reference to a process. Use wait() to wait for the "future" with a timeout. If the timeout is encounted, then cancel() the "future"
This is pretty much the only method that MATLAB provides to kill a computation without the cooperation of the computation.
Gauri Kant
Gauri Kant 2021년 6월 18일
@Walter Roberson Thanks! This actually helped me!

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


Jos (10584)
Jos (10584) 2016년 6월 7일
편집: Jos (10584) 2016년 6월 7일
You can create a timer ;
th = timer('TimerFcn','keyboard') ;
th.StartDelay = 5 ;
start(th) ;
pause ; % will be stopped after 5 seconds.
stop(th) ;
  댓글 수: 2
Gauri Kant
Gauri Kant 2021년 6월 16일
But this does not allow to move to the next iteration, does it?
Walter Roberson
Walter Roberson 2021년 6월 16일
You can have the timer routine set a shared variable that is being tested by the loop to know to move on.

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

카테고리

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