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


推荐阅读
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • MQTT技术周报:硬件连接与协议解析
    本周开发笔记重点介绍了在新项目中使用MQTT协议进行硬件连接的技术细节,涵盖其特性、原理及实现步骤。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 使用Vultr云服务器和Namesilo域名搭建个人网站
    本文详细介绍了如何通过Vultr云服务器和Namesilo域名搭建一个功能齐全的个人网站,包括购买、配置服务器以及绑定域名的具体步骤。文章还提供了详细的命令行操作指南,帮助读者顺利完成建站过程。 ... [详细]
  • 本教程涵盖OpenGL基础操作及直线光栅化技术,包括点的绘制、简单图形绘制、直线绘制以及DDA和中点画线算法。通过逐步实践,帮助读者掌握OpenGL的基本使用方法。 ... [详细]
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社区 版权所有