Home > rest_20090422 > rest_progress.m

rest_progress

PURPOSE ^

Show a progress bar for REST by Xiao-Wei Song

SYNOPSIS ^

function fout = rest_progress(x,whichbar, varargin)

DESCRIPTION ^

Show a progress bar for REST by Xiao-Wei Song
Revise from Matlab 2006b waitbar, at least 2 parameters
Usage:
rest_progress(APercent,hChild);
rest_progress(APercent,hChild, AMessage, 'Name', ATitle);
rest_progress(APercent,hParent,sprintf('Total progress:\n%s',AMessage), 'Name', ATitle);
rest_progress(APercent,hChild, theMsg, 'Name', ATitle);    
------------------------------------------------------------------------------------------------------------------------------
    Copyright(c) 2007~2010
    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
    Written by Xiao-Wei Song 
    http://resting-fmri.sourceforge.net
dawnsong, 20070520
------------------------------------------------------------------------------------------------------------------------------
    Dawnwei.Song@gmail.com
     <a href="Dawnwei.Song@gmail.com">Mail to Author</a>: Xiaowei Song
    Version=1.0;
    Release=20070903;

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

rest_progress.m

SOURCE CODE ^

0001 function fout = rest_progress(x,whichbar, varargin)
0002 %Show a progress bar for REST by Xiao-Wei Song
0003 %Revise from Matlab 2006b waitbar, at least 2 parameters
0004 %Usage:
0005 %rest_progress(APercent,hChild);
0006 %rest_progress(APercent,hChild, AMessage, 'Name', ATitle);
0007 %rest_progress(APercent,hParent,sprintf('Total progress:\n%s',AMessage), 'Name', ATitle);
0008 %rest_progress(APercent,hChild, theMsg, 'Name', ATitle);
0009 %------------------------------------------------------------------------------------------------------------------------------
0010 %    Copyright(c) 2007~2010
0011 %    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
0012 %    Written by Xiao-Wei Song
0013 %    http://resting-fmri.sourceforge.net
0014 %dawnsong, 20070520
0015 %------------------------------------------------------------------------------------------------------------------------------
0016 %    Dawnwei.Song@gmail.com
0017 %     <a href="Dawnwei.Song@gmail.com">Mail to Author</a>: Xiaowei Song
0018 %    Version=1.0;
0019 %    Release=20070903;
0020 
0021 if nargin>2
0022     if ischar(whichbar) || iscellstr(whichbar)
0023         type=2; %we are initializing
0024         msg=whichbar;
0025     elseif isnumeric(whichbar)
0026         type=1; %we are updating, given a handle
0027         f=whichbar;
0028     else
0029         error('MATLAB:waitbar:InvalidInputs', ['Input arguments of type ' class(whichbar) ' not valid.'])
0030     end
0031 elseif nargin==2    
0032     f=whichbar;
0033     if ~isnumeric(f),
0034         error('rest_progress, Input arguments not valid.');
0035     end
0036     IsExistFigure = sum(allchild(0) == f);
0037     if ~IsExistFigure
0038         type=2;
0039         msg='Waitbar';
0040     else
0041         type=1;        
0042     end
0043 else
0044     error('rest_progress, Input arguments not valid.');
0045 end
0046 
0047 if (x>=0)&&(x<=1)
0048     x = max(0,min(100*x,100));
0049 else
0050     x=-1;
0051 end    
0052 
0053 switch type
0054     case 1,  % waitbar(x)    update
0055         ProgressStartTime =getappdata(f, 'ProgressStartTime');
0056         OldPercent =getappdata(f, 'OldPercent');                
0057         if isempty(ProgressStartTime) || isempty(OldPercent) || ((x>=0)&&(OldPercent >x+3))
0058             ProgressStartTime =clock;
0059             OldPercent =0;
0060         end
0061         if x~=-1    %normal
0062             OldPercent =x;
0063         elseif ~isempty(OldPercent)        %Update GUI only, don't change progress x%
0064             x =OldPercent; %Update message  or time-msg only,x=-1
0065         else
0066             x=0;
0067         end
0068         setappdata(f, 'ProgressStartTime', ProgressStartTime);
0069         setappdata(f, 'OldPercent', OldPercent);
0070         % Update time indicator
0071         hTime = findobj(f,'Tag','DTimeIndicator');
0072         timeElapsed =etime(clock, ProgressStartTime);
0073         timeRemain  =timeElapsed *(100-x)/(1+x);
0074         theTimeMsg  =sprintf('%d%%  Elapsed %d:%d:%d, Remain %d:%d:%d    %d:%d:%d Started', ...
0075                                 floor(x), ...
0076                                 floor(timeElapsed /60 /60), ...
0077                                 mod(floor(timeElapsed /60),60) , ...
0078                                 mod(floor(timeElapsed), 60) , ...
0079                                 floor(timeRemain /60 /60), ...
0080                                 mod(floor(timeRemain /60),60), ...                                
0081                                 mod(floor(timeRemain), 60) , ...
0082                                 ProgressStartTime(4), ...
0083                                 ProgressStartTime(5), ...
0084                                 floor(ProgressStartTime(6)) );
0085         set(hTime, 'string', theTimeMsg);
0086         
0087               
0088         p = findobj(f,'Type','patch');
0089         l = findobj(f,'Type','line');
0090         if isempty(f) || isempty(p) || isempty(l),
0091             error('MATLAB:waitbar:WaitbarHandlesNotFound', 'Couldn''t find waitbar handles.');
0092         end
0093         xpatch = get(p,'XData');
0094         %Clear old first
0095         reset(p);
0096         xpatch = [0 x x 0];%Update now
0097         ypatch = [0 0 1 1];
0098         set(p,'XData',xpatch, 'YData',ypatch, 'FaceColor', 'r')
0099         xline = get(l,'XData');
0100         set(l,'XData',xline);
0101 
0102         
0103         
0104         if nargin>2,
0105             % Update Message
0106             SetMessage(f, 'DMessage',varargin{1});            
0107                         
0108             propList = varargin(2:2:end);
0109             valueList = varargin(3:2:end);
0110             
0111             for ii = 1:length( propList )
0112                 try                    
0113                     % simply set the prop/value pair of the figure
0114                     set( f, propList{ii}, valueList{ii});                    
0115                 catch
0116                     disp ( ['Warning: could not set property ''' propList{ii} ''' with value ''' num2str(valueList{ii}) '''' ] );
0117                 end
0118             end
0119         end
0120         
0121     case 2,  % waitbar(x,msg)  initialize
0122         vertMargin = 5;
0123         if nargin > 2,
0124             % we have optional arguments: property-value pairs
0125             if rem(nargin, 2 ) ~= 0
0126                 error('MATLAB:waitbar:InvalidOptionalArgsPass',  'Optional initialization arguments must be passed in pairs');
0127             end
0128         end       
0129         
0130         oldUnit =get(0, 'Units');
0131         set(0,'Units','pixels');
0132         screenSize =get(0, 'ScreenSize');
0133         set(0,'Units',oldUnit);
0134         width = 360;
0135         height = 75;
0136         %Screen Center
0137         pos = [screenSize(3)/2-width/2 screenSize(4)/2-height/2 width height];
0138 
0139         f = figure(...
0140             'Units', 'pixels', ...
0141             'BusyAction', 'queue', ...
0142             'Position', pos, ...
0143             'Resize','off', ...
0144             'CreateFcn','', ...
0145             'NumberTitle','off', ...
0146             'IntegerHandle','off', ...
0147             'MenuBar', 'none', ...
0148             'Tag','TMWWaitbar',...
0149             'Interruptible', 'off', ...
0150             'Visible','off');
0151 
0152         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0153         % set figure properties as passed to the fcn
0154         % pay special attention to the 'cancel' request
0155         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0156         visValue = 'on';
0157         if nargin > 2,
0158             propList = varargin(1:2:end);
0159             valueList = varargin(2:2:end);
0160             cancelBtnCreated = 0;
0161 
0162             visibleExist = strmatch('vis',lower(propList));
0163             if ~isempty(visibleExist)
0164                 visValue = valueList{visibleExist};
0165             end
0166 
0167             for ii = 1:length( propList )
0168                 try
0169                     if strcmpi(propList{ii}, 'CreateCancelBtn' ) && ~cancelBtnCreated
0170                         cancelBtnHeight = 25;
0171                         cancelBtnWidth = 90;
0172                         newPos = pos;
0173                         vertMargin = 5 + cancelBtnHeight+5;
0174                         newPos(4) = newPos(4)+vertMargin;
0175                         callbackFcn = [valueList{ii}];
0176                         set( f, 'Position', newPos); %, 'CloseRequestFcn', callbackFcn );
0177                         uicontrol('Parent',f, 'Tag', 'btnCancel', ... 
0178                                 'Units','pixels', ...
0179                                 'Interruptible', 'off', ...
0180                                 'Callback',callbackFcn, ...                                
0181                                 'Enable','on', ...                                
0182                                 'Position', [pos(3)-cancelBtnWidth-5, 5,  ...
0183                                 cancelBtnWidth, cancelBtnHeight], ...
0184                                 'String','Cancel');
0185                         cancelBtnCreated = 1;
0186                     else
0187                         % simply set the prop/value pair of the figure
0188                         set( f, propList{ii}, valueList{ii});
0189                     end
0190                 catch
0191                     disp ( ['Warning: could not set property ''' propList{ii} ''' with value ''' num2str(valueList{ii}) '''' ] );
0192                 end
0193             end
0194         end
0195 
0196         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0197         hTime=uicontrol('Style', 'text', 'Parent', f, ...            
0198             'Tag',     'DTimeIndicator', ...
0199             'Units', 'pixels', ...
0200             'FontSize', 10, ...
0201             'Position', [5 vertMargin pos(3)-10 15], ...
0202             'HorizontalAlignment', 'left', ...
0203             'BackgroundColor', get(f,'Color'), ...
0204             'String', 'Elapsed 0:0:0, remain 0:0:0');
0205         vertMargin =vertMargin +21 +5;    
0206 
0207         colormap([]);               
0208         axPos=[5 vertMargin pos(3)-10 15];
0209 
0210         h = axes('XLim',[0 100],...
0211             'YLim',[0 1],...
0212             'Box','on', ...
0213             'Units','pixels',...
0214             'FontSize', 16,...
0215             'Position',axPos,...
0216             'XTickMode','manual',...
0217             'YTickMode','manual',...
0218             'XTick',[],...
0219             'YTick',[],...
0220             'XTickLabelMode','manual',...
0221             'XTickLabel',[],...
0222             'YTickLabelMode','manual',...
0223             'YTickLabel',[] );
0224 
0225         
0226         hMessage=uicontrol('Style', 'text', 'Parent', f, ...            
0227             'Tag',     'DMessage', ...
0228             'Units', 'pixels', ...
0229             'FontSize', 10, ...
0230             'Position', [5 vertMargin+20 pos(3)-10 21], ...
0231             'HorizontalAlignment', 'left', ...
0232             'BackgroundColor', get(f,'Color'), ...
0233             'String', msg);
0234         SetMessage(f, 'DMessage', msg); 
0235 
0236         xpatch = [0 x x 0];
0237         ypatch = [0 0 1 1];
0238         xline = [100 0 0 100 100];
0239         yline = [0 0 1 1 0];
0240 
0241         p = patch(xpatch,ypatch,'r','EdgeColor','r','EraseMode','none');
0242         l = line(xline,yline,'EraseMode','none');
0243         set(l,'Color',get(gca,'XColor'));
0244 
0245 
0246         set(f,'HandleVisibility','callback','Visible', visValue);
0247 end  % case
0248 drawnow;
0249 
0250 if nargout==1,
0251     fout = f;
0252 end
0253 
0254 function SetMessage(AFigHandle,ATag ,AMessage)
0255     hMsg  = findobj(AFigHandle ,'Tag',ATag);
0256     set(hMsg,'string',AMessage);    
0257     
0258     theOldPosition =get(hMsg,'Position');    
0259     [newMsg,newMsgPos]=textwrap(hMsg,cellstr(AMessage));    
0260     %newMsgPos(3) =theOldPosition(3);    %the width is suppressed to be same
0261     
0262     %Adjust the Figure's Height
0263     theFigurePos =get(AFigHandle,'Position');
0264     theFigurePos(4) =theFigurePos(4) + ( newMsgPos(4)-theOldPosition(4) );    
0265     set(AFigHandle,'Position', theFigurePos);
0266     
0267     newMsgPos(3) =max(newMsgPos(3), theOldPosition(3));
0268     set(hMsg, 'String', newMsg, 'Position', newMsgPos);

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005