Breaking out from loop

Hi,
I have a for loop that run over 50 times. In the loop i'm doing cross correlation in diff images. then after then loop finish i look for the best image match.
However i find the loop take quite a long time when my image size is big. I would like to stop the loop once i get 90% match n i want my program to jump out from the loop after 2 min of execution in the loop if it is still looping after 2 min regardless there is a match or not.
i could use while loop instead of for loop but i dono how to stop the loop after 2 minutes.
Please advice.

 採用された回答

Javier
Javier 2011 年 7 月 24 日

1 投票

Hi, you can use the "tic" and "toc" matlab functions, which ones let you to create a timer and control the elapsed time between the execution of a code, for example in your case:
tStart=tic;%starts the timer
for n=1:10e50
a=n+1;%here your function
tEnd=toc(tStart);
if(tEnd>=(2*60))%checks if it has passed 2minutes
disp('It has spent 2min');
break;%break the for loop if it's true the condition
end
end
tEnd has got the time elapsed in seconds, and 10e50 is a value I have set randomly to make the code spend more than two minutes. In the condition you can see I have set "2" minutes in seconds.
Regards!

1 件のコメント

Kyle
Kyle 2011 年 7 月 25 日
Thanks

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 7 月 24 日

0 投票

It sounds like a good use case for a timer object. See this link

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2011 年 7 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by