Create waitbar without opening new window

7 次查看(过去 30 天)
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.

回答(2 个)

Joseph Cheng
Joseph Cheng 2016-6-3
编辑:Joseph Cheng 2016-6-3
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);
end
delete(hPb3)
end
delete(hPb2)
end
close(hfig)
%now go and create stuff and close stuff
end
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
hbar.Position(4)=10;
end
% if no waitbar "handle" was passed create one
if nargin<4
jPb = javax.swing.JProgressBar;
set(jPb,'StringPainted',1,'Value',12.5,'Indeterminate',0);
[hPb, hContainer] = javacomponent(jPb,[0 10*(numbar-1) 400 10],hbar);
hbar.Position(4)=10*numbar;
end
%now we should have a figure window, and waitbar "handle" lets set the value
set(hPb,'Value',value);
drawnow;
end
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 2016-6-3
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
try
if ishandle(varargin{1})
ax = varargin{1};
value = varargin{2};
p = get(ax,'Child');
x = get(p,'XData');
x(3:4) = value;
set(p,'XData',x)
return
end
pos = varargin{1};
backgroundColor = [249, 158, 0] / 255; % Orange.
foregroundColor = [0 .5 0]; % Dark Green
h = axes('Units','normalized',...
'Position',pos,...
'XLim',[0 1],'YLim',[0 1],...
'XTick',[],'YTick',[],...
'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);
WarnUser(errorMessage);
end
return; % from DisplayProgressBar()

类别

Help CenterFile Exchange 中查找有关 Dialog Boxes 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by