5

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.

user263485
  • 215
  • 3
  • 8
  • Not with the documented, builtin `waitbar`. There are alternatives however, see e.g.: http://stackoverflow.com/q/5368861/2319400 – sebastian Dec 18 '13 at 09:37

2 Answers2

5

I'd recommend multiwaitbar by Ben Tordoff, available on the MATLAB Central File Exchange.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
1

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) 
RTL
  • 3,577
  • 15
  • 24
  • Your code shows a lot of experience but the two last lines `delete(bar1)` and `delete(bar2)` doesn't work. Also it lacks an important feature **cancel button**. – user263485 Dec 18 '13 at 17:10
  • I have not yet investigated transferring the cancel button to the new image. The delete command should delete the hidden wait bars however it is not strictly required, if they were replaced (or preceded) with set(bar1,'visable','on'), and similarly for bar2, then effect could be observed. – RTL Dec 18 '13 at 17:29
  • The cancel button (including the code it executes) is is third entry of info_bar1 & info_bar2, code above is editied to include adding cancel buttons from each bar to the figure – RTL Dec 18 '13 at 17:58