热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

matlab进度条显示

在日常matlab计算时候,考虑到计算漫长的等待,要是提前知道计算时长那该多好测试代码m4;n3;p100;progressbar(1,2,3

在日常matlab计算时候,考虑到计算漫长的等待,要是提前知道计算时长那该多好

测试代码

m=4;
n=3;
p=100;
progressbar('1','2','3')
for i=1:mfor j=1:nfor k=1:ppause(0.01)face3=k/p;face2=((j-1)+face3)/n;face1=((i-1)+face2)/m;progressbar(face1,face2,face3)endend
end

progressbar进度条函数

function progressbar(varargin)
% Description:
% progressbar() provides an indication of the progress of some task using
% graphics and text. Calling progressbar repeatedly will update the figure and
% automatically estimate the amount of time remaining.
% This implementation of progressbar is intended to be extremely simple to use
% while providing a high quality user experience.
%
% Features:
% - Can add progressbar to existing m-files with a single line of code.
% - Supports multiple bars in one figure to show progress of nested loops.
% - Optional labels on bars.
% - Figure closes automatically when task is complete.
% - Only one figure can exist so old figures don't clutter the desktop.
% - Remaining time estimate is accurate even if the figure gets closed.
% - Minimal execution time. Won't slow down code.
% - Randomized color. When a programmer gets bored...
%
% Example Function Calls For Single Bar Usage:
% progressbar % Initialize/reset
% progressbar(0) % Initialize/reset
% progressbar('Label') % Initialize/reset and label the bar
% progressbar(0.5) % Update
% progressbar(1) % Close
%
% Example Function Calls For Multi Bar Usage:
% progressbar(0, 0) % Initialize/reset two bars
% progressbar('A', '') % Initialize/reset two bars with one label
% progressbar('', 'B') % Initialize/reset two bars with one label
% progressbar('A', 'B') % Initialize/reset two bars with two labels
% progressbar(0.3) % Update 1st bar
% progressbar(0.3, []) % Update 1st bar
% progressbar([], 0.3) % Update 2nd bar
% progressbar(0.7, 0.9) % Update both bars
% progressbar(1) % Close
% progressbar(1, []) % Close
% progressbar(1, 0.4) % Close
%
% Notes:
% For best results, call progressbar with all zero (or all string) inputs
% before any processing. This sets the proper starting time reference to
% calculate time remaining.
% Bar color is choosen randomly when the figure is created or reset. Clicking
% the bar will cause a random color change.
%
% Demos:
% % Single bar
% m = 500;
% progressbar % Init single bar
% for i = 1:m
% pause(0.01) % Do something important
% progressbar(i/m) % Update progress bar
% end
%
% % Simple multi bar (update one bar at a time)
% m = 4;
% n = 3;
% p = 100;
% progressbar(0,0,0) % Init 3 bars
% for i = 1:m
% progressbar([],0) % Reset 2nd bar
% for j = 1:n
% progressbar([],[],0) % Reset 3rd bar
% for k = 1:p
% pause(0.01) % Do something important
% progressbar([],[],k/p) % Update 3rd bar
% end
% progressbar([],j/n) % Update 2nd bar
% end
% progressbar(i/m) % Update 1st bar
% end
%
% % Fancy multi bar (use labels and update all bars at once)
% m = 4;
% n = 3;
% p = 100;
% progressbar('Monte Carlo Trials','Simulation','Component') % Init 3 bars
% for i = 1:m
% for j = 1:n
% for k = 1:p
% pause(0.01) % Do something important
% % Update all bars
% frac3 = k/p;
% frac2 = ((j-1) + frac3) / n;
% frac1 = ((i-1) + frac2) / m;
% progressbar(frac1, frac2, frac3)
% end
% end
% end
%
% Author:
% Steve Hoelzer
%
% Revisions:
% 2002-Feb-27 Created function
% 2002-Mar-19 Updated title text order
% 2002-Apr-11 Use floor instead of round for percentdone
% 2002-Jun-06 Updated for speed using patch (Thanks to waitbar.m)
% 2002-Jun-19 Choose random patch color when a new figure is created
% 2002-Jun-24 Click on bar or axes to choose new random color
% 2002-Jun-27 Calc time left, reset progress bar when fractiondone == 0
% 2002-Jun-28 Remove extraText var, add position var
% 2002-Jul-18 fractiondone input is optional
% 2002-Jul-19 Allow position to specify screen coordinates
% 2002-Jul-22 Clear vars used in color change callback routine
% 2002-Jul-29 Position input is always specified in pixels
% 2002-Sep-09 Change order of title bar text
% 2003-Jun-13 Change 'min' to 'm' because of built in function 'min'
% 2003-Sep-08 Use callback for changing color instead of string
% 2003-Sep-10 Use persistent vars for speed, modify titlebarstr
% 2003-Sep-25 Correct titlebarstr for 0% case
% 2003-Nov-25 Clear all persistent vars when percentdone = 100
% 2004-Jan-22 Cleaner reset process, don't create figure if percentdone = 100
% 2004-Jan-27 Handle incorrect position input
% 2004-Feb-16 Minimum time interval between updates
% 2004-Apr-01 Cleaner process of enforcing minimum time interval
% 2004-Oct-08 Seperate function for timeleftstr, expand to include days
% 2004-Oct-20 Efficient if-else structure for sec2timestr
% 2006-Sep-11 Width is a multiple of height (don't stretch on widescreens)
% 2010-Sep-21 Major overhaul to support multiple bars and add labels
%persistent progfig progdata lastupdate% Get inputs
if nargin > 0input = varargin;ninput = nargin;
else% If no inputs, init with a single barinput = {0};ninput = 1;
end% If task completed, close figure and clear vars, then exit
if input{1} == 1if ishandle(progfig)delete(progfig) % Close progress barendclear progfig progdata lastupdate % Clear persistent varsdrawnowreturn
end% Init reset flag
resetflag = false;% Set reset flag if first input is a string
if ischar(input{1})resetflag = true;
end% Set reset flag if all inputs are zero
if input{1} == 0% If the quick check above passes, need to check all inputsif all([input{:}] == 0) && (length([input{:}]) == ninput)resetflag = true;end
end% Set reset flag if more inputs than bars
if ninput > length(progdata)resetflag = true;
end% If reset needed, close figure and forget old data
if resetflagif ishandle(progfig)delete(progfig) % Close progress barendprogfig = [];progdata = []; % Forget obsolete data
end% Create new progress bar if needed
if ishandle(progfig)
else % This strange if-else works when progfig is empty (~ishandle() does not)% Define figure size and axes padding for the single bar caseheight = 0.03;width = height * 8;hpad = 0.02;vpad = 0.25;% Figure out how many bars to drawnbars = max(ninput, length(progdata));% Adjust figure size and axes padding for number of barsheightfactor = (1 - vpad) * nbars + vpad;height = height * heightfactor;vpad = vpad / heightfactor;% Initialize progress bar figureleft = (1 - width) / 2;bottom = (1 - height) / 2;progfig = figure(...'Units', 'normalized',...'Position', [left bottom width height],...'NumberTitle', 'off',...'Resize', 'off',...'MenuBar', 'none' );% Initialize axes, patch, and text for each barleft = hpad;width = 1 - 2*hpad;vpadtotal = vpad * (nbars + 1);height = (1 - vpadtotal) / nbars;for ndx = 1:nbars% Create axes, patch, and textbottom = vpad + (vpad + height) * (nbars - ndx);progdata(ndx).progaxes = axes( ...'Position', [left bottom width height], ...'XLim', [0 1], ...'YLim', [0 1], ...'Box', 'on', ...'ytick', [], ...'xtick', [] );progdata(ndx).progpatch = patch( ...'XData', [0 0 0 0], ...'YData', [0 0 1 1] );progdata(ndx).progtext = text(0.99, 0.5, '', ...'HorizontalAlignment', 'Right', ...'FontUnits', 'Normalized', ...'FontSize', 0.7 );progdata(ndx).proglabel = text(0.01, 0.5, '', ...'HorizontalAlignment', 'Left', ...'FontUnits', 'Normalized', ...'FontSize', 0.7 );if ischar(input{ndx})set(progdata(ndx).proglabel, 'String', input{ndx})input{ndx} = 0;end% Set callbacks to change color on mouse clickset(progdata(ndx).progaxes, 'ButtonDownFcn', {@changecolor, progdata(ndx).progpatch})set(progdata(ndx).progpatch, 'ButtonDownFcn', {@changecolor, progdata(ndx).progpatch})set(progdata(ndx).progtext, 'ButtonDownFcn', {@changecolor, progdata(ndx).progpatch})set(progdata(ndx).proglabel, 'ButtonDownFcn', {@changecolor, progdata(ndx).progpatch})% Pick a random color for this patchchangecolor([], [], progdata(ndx).progpatch)% Set starting time referenceif ~isfield(progdata(ndx), 'starttime') || isempty(progdata(ndx).starttime)progdata(ndx).starttime = clock;endend% Set time of last update to ensure a redrawlastupdate = clock - 1;end% Process inputs and update state of progdata
for ndx = 1:ninputif ~isempty(input{ndx})progdata(ndx).fractiondone = input{ndx};progdata(ndx).clock = clock;end
end% Enforce a minimum time interval between graphics updates
myclock = clock;
if abs(myclock(6) - lastupdate(6)) <0.01 % Could use etime() but this is fasterreturn
end% Update progress patch
for ndx &#61; 1:length(progdata)set(progdata(ndx).progpatch, &#39;XData&#39;, ...[0, progdata(ndx).fractiondone, progdata(ndx).fractiondone, 0])
end% Update progress text if there is more than one bar
if length(progdata) > 1for ndx &#61; 1:length(progdata)set(progdata(ndx).progtext, &#39;String&#39;, ...sprintf(&#39;%1d%%&#39;, floor(100*progdata(ndx).fractiondone)))end
end% Update progress figure title bar
if progdata(1).fractiondone > 0runtime &#61; etime(progdata(1).clock, progdata(1).starttime);timeleft &#61; runtime / progdata(1).fractiondone - runtime;timeleftstr &#61; sec2timestr(timeleft);titlebarstr &#61; sprintf(&#39;%2d%% %s remaining&#39;, ...floor(100*progdata(1).fractiondone), timeleftstr);
elsetitlebarstr &#61; &#39; 0%&#39;;
end
set(progfig, &#39;Name&#39;, titlebarstr)% Force redraw to show changes
drawnow% Record time of this update
lastupdate &#61; clock;% ------------------------------------------------------------------------------
function changecolor(h, e, progpatch) %#ok
% Change the color of the progress bar patch% Prevent color from being too dark or too light
colormin &#61; 1.5;
colormax &#61; 2.8;thiscolor &#61; rand(1, 3);
while (sum(thiscolor) colormax)thiscolor &#61; rand(1, 3);
endset(progpatch, &#39;FaceColor&#39;, thiscolor)% ------------------------------------------------------------------------------
function timestr &#61; sec2timestr(sec)
% Convert a time measurement from seconds into a human readable string.% Convert seconds to other units
w &#61; floor(sec/604800); % Weeks
sec &#61; sec - w*604800;
d &#61; floor(sec/86400); % Days
sec &#61; sec - d*86400;
h &#61; floor(sec/3600); % Hours
sec &#61; sec - h*3600;
m &#61; floor(sec/60); % Minutes
sec &#61; sec - m*60;
s &#61; floor(sec); % Seconds% Create time string
if w > 0if w > 9timestr &#61; sprintf(&#39;剩余&#xff1a;%d 周&#39;, w);elsetimestr &#61; sprintf(&#39;剩余&#xff1a;%d 周, %d 天&#39;, w, d);end
elseif d > 0if d > 9timestr &#61; sprintf(&#39;剩余&#xff1a;%d 天&#39;, d);elsetimestr &#61; sprintf(&#39;剩余&#xff1a;%d 天, %d 小时&#39;, d, h);end
elseif h > 0if h > 9timestr &#61; sprintf(&#39;剩余&#xff1a;%d 小时&#39;, h);elsetimestr &#61; sprintf(&#39;剩余&#xff1a;%d 小时, %d 分钟&#39;, h, m);end
elseif m > 0if m > 9timestr &#61; sprintf(&#39;剩余&#xff1a;%d 分&#39;, m);elsetimestr &#61; sprintf(&#39;剩余&#xff1a;%d 分钟, %d 秒&#39;, m, s);end
elsetimestr &#61; sprintf(&#39;剩余&#xff1a;%d 秒&#39;, s);
end

运行结果&#xff1a;


推荐阅读
author-avatar
aizhezhe
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有