waitbar within a while loop

Hi,
I have a code that need to necessary execute a "while" loop. I am wondering how is possible to use a wait bar until this while loop finishes ?
Thanks

回答 (2 件)

Brendan Hamm
Brendan Hamm 2015 年 7 月 14 日

0 投票

Right from the documentation waitbar :
h = waitbar(0,'Please wait...');
steps = 1000;
for step = 1:steps
% computations take place here
waitbar(step / steps)
end
close(h)

3 件のコメント

msh
msh 2015 年 7 月 14 日
編集済み: msh 2015 年 7 月 14 日
Thanks, but I never understood this documentation that's why I thought to ask. In a particular part of my code, there is a while loop
while > crit
do some stuff
end
this while loop I don't know when it finished, it might need 1000 or it might need 10000 iterations. I cannot tell in advance or guess how many iterations the while loop needs in order to stop.
what I am interested however, is to see a wait bar once my code enters this particular while loop and closes once the while loop stops.
So, I cannot really understand how to apply what in the documentation is suggesting, which refers to "for loop" with predetermined steps while in my case I have a "while" loop with unknown iterations.
Brendan Hamm
Brendan Hamm 2015 年 7 月 14 日
編集済み: Brendan Hamm 2015 年 7 月 14 日
Well now that you have changed the question in regards to a while-loop, the answer is that there is now way you can show the progress if you are not sure how many iterations will take place. There are of course exceptions to this depending on the criteria of your while loop. For instance if you are doing something which is converging on every iteration you could use the convergence rate for the waitbar.
David J. Mack
David J. Mack 2017 年 1 月 11 日
I agree with Brendan. The definition of the while loop is that you do not know how many steps it will take to finish (if you do, you can use a for-loop instead). Therefore, you will not be able to show the progress. Even when using convergence rate as a proxy for the steps, you are misleading the user, as an increase from 0 to 1 % might take MUCH less time than an increase from 98 to 99 %.

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

Chintan Joshi
Chintan Joshi 2017 年 1 月 11 日
編集済み: Chintan Joshi 2017 年 1 月 11 日

0 投票

I recently wanted to do it. Here's how I managed it. But you will need a counter. Below K is some vector being updated at each iteration.
c = 0;
h = waitbar(0,'Please wait...');
while c <= length(K)
steps = length(K);
% complex computations taking place! :)
c = c+1;
waitbar(c / steps);
end
close(h);

2 件のコメント

David J. Mack
David J. Mack 2017 年 1 月 11 日
Not to be picky, but this is just the for-loop mentioned by Brendan dressed in while (ignoring the index initialization error):
h = waitbar(0,'Please wait...');
steps = length(k);
for c = 1:steps
% computations take place here
waitbar(c / steps)
end
close(h)
Brendan Hamm
Brendan Hamm 2017 年 1 月 11 日
I wouldn't say this is picky. You are educating on the appropriate usage of for loops vs while loops and possibly removing any further confusion. This is simply correct!

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

カテゴリ

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

タグ

質問済み:

msh
2015 年 7 月 14 日

コメント済み:

2017 年 1 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by