how to add a waitbar in while...end loop?

4 views (last 30 days)
Jian Wang
Jian Wang on 21 Aug 2015
Commented: Joseph Cheng on 27 Aug 2015
In my code, I did not know the precise number of loop.I use therefore while...end. In addition, I want to add a waitbar in my loop, how can it be done?
  2 Comments
Adam
Adam on 21 Aug 2015
Edited: Adam on 21 Aug 2015
You will have to estimate how many iterations round the loop you expect as that is a key parameter for most progress bars. Obviously it may then jump from some semi-filled position to full (or just disappear) if it is faster than your estimate or it will sit at 100% for a while if your estimate is too low.
You can dynamically update the estimate within the loop although this can lead to progress bars that start going backwards as well as forwards which is not recommended - a pretty much guaranteed way to annoy a user and cause them to lose confidence in the software!

Sign in to comment.

Answers (1)

Joseph Cheng
Joseph Cheng on 21 Aug 2015
Depending on what your while condition is. If you are approximating maybe something like this (which converges a bit too fast to see the bar fill gradually but i think it gets the point across)
%unknown waitbar iterations test
n= [1 1];
phi = 1+1/1;
iter=1;
h=waitbar(0,['iterations: ' num2str(iter)]);
while phi/(n(end)/n(end-1))~=1
n = [n n(end)+n(end-1)];
phi = 1+1/phi;
% disp(phi/(n(end)/n(end-1)))
disp([n(end)/n(end-1) phi]);
perc= sort([n(end)/n(end-1) phi]);
iter = iter+1;
waitbar(perc(1)/perc(2),h,['iterations: ' num2str(iter)])
pause(.1);
end
waitbar(1,h,['completed with iterations: ' num2str(iter)])
Where depending on what the while loop condition is you may be able to fill it using a ratio of the while loop check. Or if you are using while 1 with a break then how close you are to the break condition.
Can you expand on the format of your while loop?
  2 Comments
Jian Wang
Jian Wang on 26 Aug 2015
Edited: Jian Wang on 26 Aug 2015
Hi Joseph,
I am sorry to reply so late. My code is as following:
function threshold=MeanPStd(data)
data=reshape(data,1,[]);
mydata=data;
M=2;
N=3;
mymean=mean(mydata);
mystd=std(mydata);
elimthreshold=mymean+N*mystd;
indices=mydata>elimthreshold;
while ~isempty(indices)
mydata(indices)=[];
mymean=mean(mydata);
mystd=std(mydata);
elimthreshold=mymean+N*mystd;
indices=mydata>elimthreshold;
end
threshold=mymean+M*mystd;
I hope you could help me. Thank you.
Joseph Cheng
Joseph Cheng on 27 Aug 2015
well first of all you have the incorrect condition for your while. indices will never be empty.
for example:
x=[1 2 3 4];
indices = x>10000000000000;
will return all zeros. what you should have is
while sum(indicies)~=0
without knowing what your data looks like it is hard to figure out how it'll converge. however if it is pretty much a normal distribution with outliers greater than 3 sigma (single sided) then you can probably see that the first iteration will get rid of the most. Everything else will then converge toward zero so you can then use something like
waitbar(1-sum(indices)/starting,hbar,['iteration #' num2str(iter)])
See attaches test code i threw together:
% function threshold=MeanPStd(data)
data = randi(100,10000,1).*randi(100,10000,1).*(-1).^(randi(2,10000,1)-1);
data = [data;randi(100000,1000,1).*(-1).^(randi(2,1000,1)-1)];
h=histfit(data,200)
data=reshape(data,1,[]);
mydata=data;
M=2;
N=3;
mymean=mean(mydata);
mystd=std(mydata);
elimthreshold=mymean+N*mystd;
indices=mydata>elimthreshold | mydata<-elimthreshold;
iter = 1;
starting = sum(indices);
hbar=waitbar(0,'processing');
while sum(indices)~=0
mydata(indices)=[];
histfig=histfit(mydata,2000);
title(iter)
iter=iter+1;
pause(1)
mymean=mean(mydata);
mystd=std(mydata);
elimthreshold=mymean+N*mystd;
indices=mydata>elimthreshold | mydata<-elimthreshold;
waitbar(1-sum(indices)/starting,hbar,['iteration #' num2str(iter)])
end
waitbar(1,hbar,['completed in iteration #' num2str(iter)])
threshold=mymean+M*mystd;

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!