Create waitbar without opening new window

15 views (last 30 days)
Christoph on 3 Jun 2016
Answered: Image Analyst on 3 Jun 2016
I know how to create/use a waitbar in Matlab, but what I don't like is that it opens a new window. I have a routine that runs for quite a while and opens several waitbars, so I always have a flickering window in my task bar when I have it minimized.
What I want to achieve is to have a progress indication that doesn't clog my console (at least not after the run is finished) but doesn't open a new window.

Answers (2)

Joseph Cheng
Joseph Cheng on 3 Jun 2016
Edited: Joseph Cheng on 3 Jun 2016
so... here is a quick example to create a waitbar that you can do some extra stuff by utilizing java. i'll let you figure out how to get it so that you create the progress bars downward for new waitbars or however you want to represent them.
function wbartest()
[hPb1, hfig]=joes_super_waitbar(1,0);
for ind = 1:10:100
[hPb1, hfig]=joes_super_waitbar(1,ind,hfig,hPb1);
[hPb2, hfig]=joes_super_waitbar(2,0,hfig);
for jnd = 1:10:100
[hPb2, hfig]=joes_super_waitbar(2,jnd,hfig,hPb2);
[hPb3, hfig]=joes_super_waitbar(3,0,hfig);
for knd = 1:1:100
[hPb3, hfig]=joes_super_waitbar(3,knd,hfig,hPb3);
%now go and create stuff and close stuff
function [hPb , hbar]=joes_super_waitbar(numbar,value,hbar,hPb)
%create figure if only the bar number and it's value is inputted.
if nargin<3
hbar =figure(100); %some large figure number.
%I kept it the same so i didn't have to keep closing figures
%or having lots of open figures during my quick test
% if no waitbar "handle" was passed create one
if nargin<4
jPb = javax.swing.JProgressBar;
[hPb, hContainer] = javacomponent(jPb,[0 10*(numbar-1) 400 10],hbar);
%now we should have a figure window, and waitbar "handle" lets set the value
or..... you could just pass the waitbar created handles to the new waitbar instance overwriting the current waitbar status and text. This way you don't create a new waitbar but instead update it most recent progress. You'll have to figure out what parent progress you have to revert to when you used to close the hbar.

Image Analyst
Image Analyst on 3 Jun 2016
Here's what I use. It draws the wait bar / progress bar right on your main GUI at a location you specify (See the comments):
% Displays a progress bar on the figure at the specified location.
function h = DisplayProgressBar(varargin)
%DisplayProgressBar: A progress bar that can be embedded in a GUI figure.
% Syntax and sample calling:
% progressBarPosition = [.376 .414 .198 .052]; % Position of progress bar in normalized units.
% handleToProgressBar = DisplayProgressBar(progressBarPosition);
% for k = 1:100
% percentageDone = k / 100;
% DisplayProgressBar(handleToProgressBar, percentageDone)
% end
% % Delete the progress bar. Make it disappear.
% delete(handleToProgressBar);
% Written by Doug Schwarz, 11 December 2008
if ishandle(varargin{1})
ax = varargin{1};
value = varargin{2};
p = get(ax,'Child');
x = get(p,'XData');
x(3:4) = value;
pos = varargin{1};
backgroundColor = [249, 158, 0] / 255; % Orange.
foregroundColor = [0 .5 0]; % Dark Green
h = axes('Units','normalized',...
'XLim',[0 1],'YLim',[0 1],...
'Color', backgroundColor,...
'XColor', backgroundColor,'YColor', backgroundColor);
patch([0 0 0 0], [0 1 1 0], foregroundColor,...
'Parent', h,...
'EdgeColor', 'none');
% set(h, 'units', 'normalized');
catch ME
callStackString = GetCallStack(ME);
errorMessage = sprintf('Error in program %s.\nTraceback (most recent at top):\n%s\nError Message:\n%s', ...
mfilename, callStackString, ME.message);
return; % from DisplayProgressBar()


Community Treasure Hunt

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

Start Hunting!