Is it possible to have 2 (multiple) waitbars (progressbar) in matlab? Something like two bars in a window.
I know we can implement it using a user defined GUI and do everything manually but that would be a long way.
I'd recommend multiwaitbar by Ben Tordoff, available on the MATLAB Central File Exchange.
If being in the same figure window is not essential then give the waitbars handles and call them by the handle; e.g.
%% Script creating 2 wait bars in different windows
bar1=waitbar(0,'bar1'); % creates 2 waitbars
bar2=waitbar(0,'bar2');
% updates bar2
waitbar(completed_value_bar2,bar2,'updated message') % updated message is optional
% updates bar1
waitbar(completed_value_bar1,bar1,'updated message') % updated message is optional
%
delete(bar1)
delete(bar2)
If it is essential it is possible using the following method but increases runtime horribly
%% Script creating 2 wait bars in the same figure window
bar1=waitbar(0,'this is bar1','CreateCancelBtn','foo1');
bar2=waitbar(0,'this is bar2','CreateCancelBtn','foo2');
% foo1 represents function executed by cancel button 1 (similar for foo2)
Pos=get(bar1,'OuterPosition');
info_bar1=findobj(bar1); % gets waitbar object handles
info_bar2=findobj(bar2); %
set(bar1,'visible','off') % hides the bars
set(bar2,'visible','off') %
% generates intital figure window;
F=figure;
set(F,'position',[Pos(1:2),1.35*Pos(3),2*Pos(4)]); % resises figure (could be more elegant)
loc1=get(info_bar1(2),'position'); % get position for bar1
loc2=loc1+[0 50 0 0]; % shifts bar2 up
P = copyobj(info_bar1(2),F); % Copy bar1 to new fig
% note the figure handle bar1(2) contains the waitbar & message for bar1
set(P,'position',loc1) % Sets position of bar 1
Q = copyobj(info_bar2(2),F); % Copy bar2 to new fig
set(Q,'position',loc2) % Sets position of bar 1
button_loc1=get(info_bar1(3),'position'); % gets button location
button_loc2=button_loc1+[0 50 0 0]; % shifts button 2
B1 = copyobj(info_bar1(3),F); % adds buttons to figure
set(B1,'position',button_loc1) % sets button location
B2 = copyobj(info_bar2(3),F);
set(B2,'position',button_loc2)
%
for a=1:100
for b=1:100
%some calculation
% updates bar2
completed_value_bar2=b/100;
waitbar(completed_value_bar2,bar2,'updated message')
delete(Q)
Q = copyobj(info_bar2(2),F);
set(Q,'position',loc2)
end
% updates bar1
completed_value_bar1=a/100;
waitbar(completed_value_bar1,bar1,'updated message')
delete(P)
P = copyobj(info_bar1(2),F);
set(P,'position',loc1)
end
%
delete(bar1)
delete(bar2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With