{"id":101,"date":"2007-08-01T10:00:19","date_gmt":"2007-08-01T15:00:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/08\/01\/monitoring-progress-of-a-calculation\/"},"modified":"2018-01-08T16:11:13","modified_gmt":"2018-01-08T21:11:13","slug":"monitoring-progress-of-a-calculation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/08\/01\/monitoring-progress-of-a-calculation\/","title":{"rendered":"Monitoring Progress of a Calculation"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Users sometimes want to monitor the progress of some calculation or simulation.  Here's a recent example from the <a>MATLAB newsgroup<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#2\">Graphical Output Option Using waitbar<\/a><\/li>\r\n         <li><a href=\"#7\">Textual Output Option<\/a><\/li>\r\n         <li><a href=\"#11\">Progress Indicators<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Some functions have an option you can set that displays information throughout the course of a calculation, for example, <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fminsearch.html\"><tt>fminsearch<\/tt><\/a> via the <tt>OutputFcn<\/tt> in <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/optimset.html\"><tt>optimset<\/tt><\/a>. What are your choices when you want some intermediate output from your own code?  I see three choices.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Mimic behavior of the <tt>OutputFcn<\/tt> idea in your own code so you can specify the output.\r\n         <\/li>\r\n         <li>Use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/waitbar.html\"><tt>waitbar<\/tt><\/a> for a graphic progress meter.\r\n         <\/li>\r\n         <li>Send output to the command window in a way that's legible and doesn't clutter the information, using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fprintf.html\"><tt>fprintf<\/tt><\/a> and taking advantage of some of the special formatting capabilities.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Graphical Output Option Using waitbar<a name=\"2\"><\/a><\/h3>\r\n   <p>Here's a very short illustration of the kind of control you can get using <tt>waitbar<\/tt>.  First, create a <tt>waitbar<\/tt> and get its handle.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h = waitbar(0,<span style=\"color: #A020F0\">'Initializing waitbar...'<\/span>);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/101\/printWoverwrite_01.png\"> <p>Here's an update when the calculation is halfway done.  Make sure to use the <tt>waitbar<\/tt> handle to update the existing <tt>waitbar<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">waitbar(0.5,h,<span style=\"color: #A020F0\">'Halfway there...'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/101\/printWoverwrite_02.png\"> <p>And further along again:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">perc = 75;\r\nwaitbar(perc\/100,h,sprintf(<span style=\"color: #A020F0\">'%d%% along...'<\/span>,perc))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/101\/printWoverwrite_03.png\"> <p>Close the progess meter when the calculation is done.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">close(h)<\/pre><p>There are a plethora of alternatives to <tt>waitbar<\/tt> on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\">The MATLAB Central File Exchange<\/a>. I count 29 today when I search for <tt>waitbar<\/tt>.\r\n   <\/p>\r\n   <h3>Textual Output Option<a name=\"7\"><\/a><\/h3>\r\n   <p><tt>fprintf<\/tt> can print output to the command window by specifying the file identifier as the value <tt>1<\/tt>.  In addition, you can take advantage of escape characters, such as <tt>\\b<\/tt> for backspace, to control where the output goes.\r\n   <\/p>\r\n   <p>Look at lines 2 and 4 in the code snippet here.  On line 2, I print out a line, but do not terminate it with a newline character\r\n      <tt>\\n<\/tt>.  On line 4, we use the backspace capability to overwrite our previous integer, before pausing so the command window output\r\n      can be updated.  Finally, when the loop is complete, I use <tt>fprintf<\/tt> to advance the cursor to a new line <tt>\\n<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dbtype <span style=\"color: #A020F0\">cmdwinProgress<\/span>\r\nclc<\/pre><pre style=\"font-style:oblique\">\r\n1     % Example Code for Printing Progress to the Command Window\r\n2     fprintf(1,'here''s my integer:  ');\r\n3     for i=1:9\r\n4         fprintf(1,'\\b%d',i); pause(.1)\r\n5     end\r\n6     fprintf('\\n')\r\n<\/pre><p>Here's the final result from that command window, having first made sure the screen (using <tt>clc<\/tt>) before running <tt>cmdwinProgress<\/tt>.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/101\/cmdwin.jpg\"> <\/p>\r\n   <h3>Progress Indicators<a name=\"11\"><\/a><\/h3>\r\n   <p>What do you look for in a progress indicator?  Do any of the options mentioned above work for you?  If not, why not?  Post\r\n      your thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=101#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_cca6ade899ab4ab48fe66464c2eed116() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='cca6ade899ab4ab48fe66464c2eed116 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' cca6ade899ab4ab48fe66464c2eed116';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_cca6ade899ab4ab48fe66464c2eed116()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.4<br><\/p>\r\n<\/div>\r\n<!--\r\ncca6ade899ab4ab48fe66464c2eed116 ##### SOURCE BEGIN #####\r\n%% Monitoring Progress of a Calculation\r\n% Users sometimes want to monitor the progress of some calculation or\r\n% simulation.  Here's a recent example from the \r\n% <http:\/\/view_thread\/152616 MATLAB newsgroup>.\r\n%\r\n%% \r\n% Some functions have an option you can set that displays information\r\n% throughout the course of a calculation, for example, \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fminsearch.html |fminsearch|>\r\n% via the |OutputFcn| in\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/optimset.html |optimset|>.\r\n% What are your choices when you want some intermediate output from your\r\n% own code?  I see three choices.\r\n%\r\n% * Mimic behavior of the |OutputFcn| idea in your own code so you can\r\n% specify the output.\r\n% * Use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/waitbar.html |waitbar|>\r\n% for a graphic progress meter.\r\n% * Send output to the command window in a way that's legible and doesn't\r\n% clutter the information, using\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fprintf.html |fprintf|>\r\n% and taking advantage of some of the special formatting capabilities.\r\n\r\n%% Graphical Output Option Using waitbar\r\n% Here's a very short illustration of the kind of control you can get using\r\n% |waitbar|.  First, create a |waitbar| and get its handle.\r\nh = waitbar(0,'Initializing waitbar...');\r\n%%\r\n% Here's an update when the calculation is halfway done.  Make sure to use\r\n% the |waitbar| handle to update the existing |waitbar|.\r\nwaitbar(0.5,h,'Halfway there...')\r\n%%\r\n% And further along again:\r\nperc = 75;\r\nwaitbar(perc\/100,h,sprintf('%d%% along...',perc))\r\n%%\r\n% Close the progess meter when the calculation is done.\r\nclose(h)\r\n%%\r\n% There are a plethora of alternatives to |waitbar| on \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange The MATLAB Central File Exchange>.\r\n% I count 29 today when I search for |waitbar|.\r\n%% Textual Output Option\r\n% |fprintf| can print output to the command window by specifying the file\r\n% identifier as the value |1|.  In addition, you can take advantage of\r\n% escape characters, such as |\\b| for backspace, to control where the\r\n% output goes.  \r\n%%\r\n% Look at lines 2 and 4 in the code snippet here.  On line 2,\r\n% I print out a line, but do not terminate it with a newline character\r\n% |\\n|.  On line 4, we use the backspace capability to overwrite our\r\n% previous integer, before pausing so the command window output can be\r\n% updated.  Finally, when the loop is complete, I use |fprintf| to advance\r\n% the cursor to a new line |\\n|.\r\ndbtype cmdwinProgress\r\nclc\r\n%%\r\n% Here's the final result from that command window, having first made sure\r\n% the screen (using |clc|) before running |cmdwinProgress|.\r\n%%\r\n%\r\n% <<cmdwin.jpg>>\r\n%%  Progress Indicators\r\n% What do you look for in a progress indicator?  Do any of the options\r\n% mentioned above work for you?  If not, why not?  Post your thoughts\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=101#respond here>.\r\n##### SOURCE END ##### cca6ade899ab4ab48fe66464c2eed116\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Users sometimes want to monitor the progress of some calculation or simulation.  Here's a recent example from the MATLAB newsgroup.\r\n      \r\n   \r\n   Contents\r\n   \r\n    ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/08\/01\/monitoring-progress-of-a-calculation\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/101"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":2602,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/101\/revisions\/2602"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}