📄 progressbar.m
字号:
'Selected', 'off', ...
'SelectionHighlight', 'off', ...
'TickLength', [.005 0], ...
'Visible', 'on', ...
'XLim', [0 1], ...
'XTick', [.25 .50 .75], ...
'XTickLabel', [], ...
'YLim', [0 2], ...
'YTick', [], ...
'YTickLabel', [], ...
'PlotBoxAspectRatio', [1 .025 1] ...
);
%Create progress bar filler - thick line works fastest
ud.h.line = line( ...
'Tag', 'Progress Bar Line', ...
'Color', [1 0 0], ...
'EraseMode', 'normal', ...
'HitTest', 'off', ...
'LineStyle', '-', ...
'LineWidth', 1000, ...
'Marker', 'none', ...
'Parent', ud.h.axes, ...
'Selected', 'off', ...
'SelectionHighlight', 'off', ...
'Visible', 'on', ...
'XData', [0 ud.X], ...
'YData', [1 1] ...
);
set(ud.h.pbar, 'UserData', ud);
%Set approprate fields by making initial update
progressbar('title', ud.h.pbar, varargin{1});
progressbar('message', ud.h.pbar, varargin{2});
progressbar('update', ud.h.pbar, ud.X);
varargout{1} = ud.h.pbar;
case 'cancelbutton'
%Create cancel button
%usage: progressbar('cancelbutton', h)
ud = get(varargin{1}, 'UserData');
%Create default callback
cancelcallback = ['set(get(gcbo, ''Parent''), ' ...
'''UserData'', ' ...
'setfield(get(get(gcbo, ''Parent''), ''UserData''), ' ...
'''stop'', ' ...
'1' ...
')' ...
'); ' ...
];
%Append user callback
if nargin == 3
cancelcallback = [cancelcallback varargin{2}];
end
%Shift all object in figure up
tmph=get(ud.h.pbar, 'Children');
tmppos = get(tmph, 'Position');
for n = 1:length(tmph)
set(tmph(n), ...
'Position', ...
(tmppos{n}/ud.winscale + [0 60 0 0]) * ud.winscale ...
);
end
clear tmph tmppos n;
%Resize figure to make room for cancel button
set(ud.h.pbar, ...
'Position', ...
(get(ud.h.pbar, 'Position')/ud.winscale + [0 0 0 60]) * ud.winscale ...
);
%Create cancel button in new space
ud.h.cancelbutton = uicontrol( ...
'Tag', 'Cancel Button', ...
'Style', 'pushbutton', ...
'String', 'Cancel', ...
'TooltipString', '', ...
'Enable', 'on', ...
'Callback', cancelcallback, ...
'Units', 'pixels', ...
'Position', ([200 10 200 50] * ud.winscale), ...
'Parent', ud.h.pbar, ...
'SelectionHighlight', 'off', ...
'Selected', 'off', ...
'FontName', 'FixedWidth', ...
'FontSize', 1, ...
'FontUnits', 'pixels', ...
'Visible', 'on' ...
);
drawnow;
set(ud.h.pbar, 'UserData', ud);
case 'update'
%update display (if necessary)
%usage: progressbar('update', h, X)
ud = get(varargin{1}, 'UserData');
%Calculate delta X and delta time
oldX = ud.X;
oldtime = ud.time.current;
newX = min(max(varargin{2}, 0), 1); %Force input X to be between 0 and 1
newtime = clock;
difftime = etime(newtime, oldtime);
diffX = newX - oldX;
%if last update was more than refresh rate seconds ago,
%or if X is 100% or 0%, then update, else do nothing
if (difftime >= ud.refresh) | (newX == 1) | (newX == 0)
%Update appropriate UserData fields
ud.time.current = newtime;
ud.time.elapsed = progressbar('sec2time', etime(ud.time.current, ud.time.start));
if diffX < eps %The estimate will be too large, so set to 0's -> ??:??:??
ud.time.estimated = [0 0 0 0 0 0];
else %Linear estimate
ud.time.estimated = progressbar('sec2time', (difftime/diffX)*(1-newX));
end
ud.X = newX;
set(ud.h.pbar, 'UserData', ud);
%Update status string and progress bar
statusstr = [ ...
'Start: ' ...
progressbar('time2str', ud.time.start) ...
sprintf('\t') ...
'Elapsed: ' ...
progressbar('time2str', ud.time.elapsed) ...
sprintf('\n') ...
'Estimated: ' ...
progressbar('time2str', ud.time.estimated) ...
sprintf('\t') ...
'Completed: ' ...
sprintf(' %06.2f%%', ud.X*100) ...
];
set(ud.h.line, 'XData', [0 ud.X]);
set(ud.h.status, 'String', statusstr);
drawnow;
end
case 'message'
%Change message
%usage: progressbar('message', h, 'Message')
ud = get(varargin{1}, 'UserData');
set(ud.h.message, 'String', varargin{2});
case 'title'
%Change title
%usage: progressbar('title', h, 'Title')
set(varargin{1}, 'Name', varargin{2});
case 'refresh'
%Change refresh rate (default is .1 sec)
%useage: progressbar('refresh', h, refreshrate)
ud = get(varargin{1}, 'UserData');
ud.refresh = varargin{2};
set(ud.h.pbar, 'UserData', ud);
case 'status'
%Get status (UserData) structure
%usage: status = progressbar('status', h)
%Yes, this is redundant, but it is included for completeness.
varargout{1} = get(varargin{1}, 'UserData');
case 'kill'
%Close progressbar
%usage: progressbar('kill', h)
%Yes, this is redundant as well...
close(varargin{1});
case 'sec2time'
%Convert seconds (usually from etime) to datevector
%usage: datevec = progressbar('sec2time', t)
%We don't care about month/day/year, so...
varargout{1} = [0 ...
0 ...
0 ...
floor(varargin{1}/3600) ...
mod(floor(varargin{1}/60), 60) ...
mod(varargin{1}, 60) ...
];
case 'time2str'
%Convert datevector to HH:MM:SS time string
%usage: timestr = progressbar('time2str', datevec)
if any(varargin{1}(5:6) > [60 60]) %Make corrections for min/sec overflow
varargin{1} = progressbar('sec2time', sum(varargin{1}(4:6) .* [3600 60 1]));
end
%Return '??:??:??' for invalid datevectors
if all(varargin{1}(4:6) == 0) ...
| any(varargin{1} < 0) ...
| any(varargin{1} > [Inf Inf Inf 99 60 60])
varargout{1} = ['??:??:??'];
else %If everything is legit, return HH:MM:SS string
varargout{1} = sprintf('%02.0f:%02.0f:%02.0f', ...
varargin{1}(4), ...
varargin{1}(5), ...
varargin{1}(6) ...
);
end
otherwise
error('Unrecognized action');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -