热门标签 | 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;


推荐阅读
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了logistic回归(线性和非线性)相关的知识,包括线性logistic回归的代码和数据集的分布情况。希望对你有一定的参考价值。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 本文介绍了Java后台Jsonp处理方法及其应用场景。首先解释了Jsonp是一个非官方的协议,它允许在服务器端通过Script tags返回至客户端,并通过javascript callback的形式实现跨域访问。然后介绍了JSON系统开发方法,它是一种面向数据结构的分析和设计方法,以活动为中心,将一连串的活动顺序组合成一个完整的工作进程。接着给出了一个客户端示例代码,使用了jQuery的ajax方法请求一个Jsonp数据。 ... [详细]
  • 本文总结了在编写JS代码时,不同浏览器间的兼容性差异,并提供了相应的解决方法。其中包括阻止默认事件的代码示例和猎取兄弟节点的函数。这些方法可以帮助开发者在不同浏览器上实现一致的功能。 ... [详细]
  • [echarts] 同指标对比柱状图相关的知识介绍及应用示例
    本文由编程笔记小编为大家整理,主要介绍了echarts同指标对比柱状图相关的知识,包括对比课程通过率最高的8个课程和最低的8个课程以及全校的平均通过率。文章提供了一个应用示例,展示了如何使用echarts制作同指标对比柱状图,并对代码进行了详细解释和说明。该示例可以帮助读者更好地理解和应用echarts。 ... [详细]
  • 我用Tkinter制作了一个图形用户界面,有两个主按钮:“开始”和“停止”。请您就如何使用“停止”按钮终止“开始”按钮为以下代码调用的已运行功能提供建议 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • 网址:https:vue.docschina.orgv2guideforms.html表单input绑定基础用法可以通过使用v-model指令,在 ... [详细]
  • 开源Keras Faster RCNN模型介绍及代码结构解析
    本文介绍了开源Keras Faster RCNN模型的环境需求和代码结构,包括FasterRCNN源码解析、RPN与classifier定义、data_generators.py文件的功能以及损失计算。同时提供了该模型的开源地址和安装所需的库。 ... [详细]
  • Python使用Pillow包生成验证码图片的方法
    本文介绍了使用Python中的Pillow包生成验证码图片的方法。通过随机生成数字和符号,并添加干扰象素,生成一幅验证码图片。需要配置好Python环境,并安装Pillow库。代码实现包括导入Pillow包和随机模块,定义随机生成字母、数字和字体颜色的函数。 ... [详细]
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社区 版权所有