Code covered by the BSD License  

Highlights from
ProgressBar

image thumbnail

ProgressBar

by

 

15 Oct 2009 (Updated )

Graphical progress bar, containing an arbitrary number of bars.

progressbar(varargin)
function pb = progressbar(varargin)
% PROGRESSBAR - Create an instance of a PROGRESSBAR object.
% 
% PB = PROGRESSBAR(N)
% Create PROGRESSBAR instance with N bars. If omitted, N=1.
% 
% PB = PROGRESSBAR(PARAM1,VALUE1,PARAM2,VALUE2,...)
% PB = PROGRESSBAR(N,PARAM1,VALUE1,PARAM2,VALUE2,...)
% Pass PARAM/VALUE pairs to the FIGURE() call upon creation.
% e.g.
%  p = progressbar('Position',[5 3 10 20])
%
% See also: SETPROGRESS, SET, GET
%
% Example:
%
% pb = progressbar();
% N = 100;
% for ii=1:N
%   pause(.02) %simulate processing time
%   setProgress(pb,ii/N);
% end
% close(pb) 
%
% ~~~~
% Author: Andrew Schwartz
% Harvard/MIT SHBT program
% Version 1.0.1, 10/16/2009
%
% 10/16/09 - fixed bug with figure format parameter inputs

%% parse input
if nargin<1
    n=1;
else
    if isa(varargin{1},'char')
        n=1;
    else
        n = varargin{1};
        varargin(1)=[];
    end
end

%% create figure
pb.handle = figure( ...
    'Color',[.9 .9 .9],...
    'Units','characters',...
    'MenuBar','none',...
    'DockControls','off',...
    'IntegerHandle','off',...
    'Resize','off',...
    'Toolbar','none',...
    'NumberTitle','off',...
    'Name','Progress' ...
);

p = get(pb.handle,'Position');
set(pb.handle,'Position',[p(1:2) 40 3*n+1])
if ~isempty(varargin), set(pb,varargin{:}); end %varargin may override 'posotion' attribute
p = get(pb.handle,'Position');  %if 'position' overridden, must re-read
w = p(3);
h = p(4);

%% graphical objects
pb.bar = zeros(1,n);
pb.bartext = pb.bar;

for ii=1:n
    pb.bar(ii) = axes(...
        'Parent',pb.handle,...
        'Units','Characters',...
        'Position',[1, (h-(3*ii)), w-2, 1],...
        'Color',[1 1 1],...
        'XTick',[],...
        'YTick',[],...
        'Box','on'...
    );
    pb.bartext(ii) = uicontrol(pb.handle,...
        'Style','Text',...
        'BackgroundColor',get(pb.handle,'Color'),...
        'Units','characters',...
        'Position',[0, (h-(3*ii-1)),w,1],...
        'HorizontalAlignment','center',...
        'String','Please wait...'...
    );
end

%% class members
pb.n = n;
pb.currentBar = 1;
pb.currentProgress = zeros(n,1);
pb.fcolor = [0 0 1];
pb.bcolor = [1 1 1];
pb.UserData = [];

pb = class(pb,'progressbar');

Contact us