1

I am trying to add a dynamic progress bar in a GUI. I note there are some solutions available (How to add progress bar control to Matlab gui?). My method is based on creating two panels of different colors, one for background and the other for foreground (i.e., progress bar). My codes are as follows:

bar = uipanel('Parent',handles.bgProgressBar,'BackgroundColor','r');
%Note: bgPogressBar is the tag of a panel manually added with GUIDE
barPosition = get(bar,'Position');
cnt = 0
for ii = 1:S
   for jj = 1:T

       do something 
       ….

       cnt = cnt + 1;
       progress = cnt/(S*T);
       barPosition(3) = progress;
       barPosition;
       set(bar,'Position',barPosition);   
   end
end

The problem here is that the bar is not updated in real time. It does not repond but only progress to the end when the loop is completed. Is it possible to add a dynamically progressing bar in the GUI?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
jwm
  • 4,832
  • 10
  • 46
  • 78

1 Answers1

1

Use drawnow after set in order to update the graphic object on screen immediately:

bar = uipanel('Parent',handles.bgProgressBar,'BackgroundColor','r');
%Note: bgPogressBar is the tag of a panel manually added with GUIDE
barPosition = get(bar,'Position');
cnt = 0
for ii = 1:S
   for jj = 1:T

       do something 
       ….

       cnt = cnt + 1;
       progress = cnt/(S*T);
       barPosition(3) = progress;
       barPosition;
       set(bar,'Position',barPosition);   
       drawnow %%%%%
   end
end
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • I have a short follow-up question. I found the same problem when I replaced the progressbar with a text indicator in the GUI. Do you have idea of dynamically updating a text string? still drawnow? – jwm Dec 14 '18 at 00:33
  • I have verified that 'drawnow' works for updating text strings as well! – jwm Dec 14 '18 at 00:36
  • Yes, `drawnow` updates figures in general. If the figure contains a `text` object it should also get updated – Luis Mendo Dec 14 '18 at 00:44