{"id":5589,"date":"2014-10-24T09:47:23","date_gmt":"2014-10-24T13:47:23","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5589"},"modified":"2014-10-24T09:47:23","modified_gmt":"2014-10-24T13:47:23","slug":"stop","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/10\/24\/stop\/","title":{"rendered":"STOP!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35074-display-progress--pause-or-stop-a-time-consuming-loop\">Display progress, pause or stop a time-consuming loop<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/115634\">Rafael Oliveira<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Background<a name=\"1\"><\/a><\/h3>\r\n   <p>It is not uncommon to see questions such asking <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/?term=stop+for+loop+button\">\"How do I stop a <tt>for<\/tt> or <tt>while<\/tt> loop when a user pushes a button?\"<\/a> in <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/\">MATLAB Answers<\/a>.  There are a few ways to do this, but usually the results involve checking the state of a toggle button or some flag that\r\n      has been set by a user. Alternatively, one could consider replacing the <tt>for<\/tt> or <tt>while<\/tt> loop with a <tt>timer<\/tt> which is what I typically recommend.\r\n   <\/p>\r\n   <p>Rafael's <tt>stop<\/tt> makes stopping a loop easy and elegant to do.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> ii = 1:100\r\n    <span style=\"color: #0000FF\">if<\/span> ~stop.requested\r\n        disp([<span style=\"color: #A020F0\">'Still Running, iteration: '<\/span> num2str(ii)])\r\n\r\n        <span style=\"color: #228B22\">% Computationally expensive operation.<\/span>\r\n        fft2(rand(5000));\r\n    <span style=\"color: #0000FF\">else<\/span>\r\n        <span style=\"color: #228B22\">% I clicked said stop<\/span>\r\n        disp(<span style=\"color: #A020F0\">'Stopped'<\/span>)\r\n        <span style=\"color: #0000FF\">break<\/span>\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n    drawnow\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Still Running, iteration: 1\r\nStill Running, iteration: 2\r\nStill Running, iteration: 3\r\nStill Running, iteration: 4\r\nStill Running, iteration: 5\r\nStill Running, iteration: 6\r\nStill Running, iteration: 7\r\nStopped\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainformonitor\/mainformonitor_01.png\"> <p>This utility also allows you to pause and resume elegantly.<\/p>\r\n   <h3>How I found it?<a name=\"3\"><\/a><\/h3>\r\n   <p>I typically find File Exchange submissions either by randomly browsing, needing some piece of functionality, or trying to\r\n      help someone not reinvent the wheel.  However, I came by this one a little differently. Out of curiosity, I wanted to know\r\n      which File Exchange submissions contain the string \"xkcd\".  This one came up with a nice little easter egg related to a <a href=\"http:\/\/www.xkcd.com\/1017\/\">specific comic<\/a>.\r\n   <\/p>\r\n   <p>Turning that mode on:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> ii = 0:0.3:1\r\n    stop.setStatus(<span style=\"color: #A020F0\">'xkcd'<\/span>,ii)\r\n    drawnow\r\n    snapnow\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainformonitor\/mainformonitor_02.png\"> <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainformonitor\/mainformonitor_03.png\"> <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainformonitor\/mainformonitor_04.png\"> <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainformonitor\/mainformonitor_05.png\"> <h3>Comments<a name=\"4\"><\/a><\/h3>\r\n   <p>What approaches do you usually take to allow for user interactions with a loop or timer?<\/p>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5589#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35074-display-progress--pause-or-stop-a-time-consuming-loop#comments\">comment<\/a> for Rafael.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_ef19192d9219409a9e924a26830026f5() {\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='ef19192d9219409a9e924a26830026f5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' ef19192d9219409a9e924a26830026f5';\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 = 'Sean de Wolski';\r\n        copyright = 'Copyright 2014 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_ef19192d9219409a9e924a26830026f5()\"><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; R2014b<br><\/p>\r\n<\/div>\r\n<!--\r\nef19192d9219409a9e924a26830026f5 ##### SOURCE BEGIN #####\r\n%% STOP!\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35074-display-progress--pause-or-stop-a-time-consuming-loop Display progress, pause or stop a time-consuming loop> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/115634 Rafael Oliveira>.\r\n% \r\n\r\n%% Background\r\n%\r\n% It is not uncommon to see questions such asking <https:\/\/www.mathworks.com\/matlabcentral\/answers\/?term=stop+for+loop+button \"How do I stop a |for| or |while| loop when a user pushes a button?\"> in\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/ MATLAB Answers>.  There\r\n% are a few ways to do this, but usually the results involve checking the\r\n% state of a toggle button or some flag that has been set by a user.\r\n% Alternatively, one could consider replacing the |for| or |while| loop\r\n% with a |timer| which is what I typically recommend.\r\n%\r\n% Rafael's |stop| makes stopping a loop easy and elegant to do.\r\n%\r\n\r\nfor ii = 1:100\r\n    if ~stop.requested\r\n        disp(['Still Running, iteration: ' num2str(ii)])\r\n        \r\n        % Computationally expensive operation.        \r\n        fft2(rand(5000));\r\n    else\r\n        % I clicked said stop\r\n        disp('Stopped')\r\n        break\r\n    end\r\n    drawnow\r\nend\r\n\r\n%%\r\n% This utility also allows you to pause and resume elegantly.\r\n%  \r\n\r\n\r\n%% How I found it?\r\n%\r\n% I typically find File Exchange submissions either by randomly browsing,\r\n% needing some piece of functionality, or trying to help someone not\r\n% reinvent the wheel.  However, I came by this one a little differently.\r\n% Out of curiosity, I wanted to know which File Exchange submissions\r\n% contain the string \"xkcd\".  This one came up with a nice little easter\r\n% egg related to a <http:\/\/www.xkcd.com\/1017\/ specific comic>.\r\n%\r\n% Turning that mode on:\r\n\r\nfor ii = 0:0.3:1\r\n    stop.setStatus('xkcd',ii)\r\n    drawnow\r\n    snapnow    \r\nend\r\n\r\n%% Comments\r\n% \r\n% What approaches do you usually take to allow for user interactions with a\r\n% loop or timer?\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5589#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/35074-display-progress--pause-or-stop-a-time-consuming-loop#comments\r\n% comment> for Rafael.\r\n%\r\n \r\n\r\n##### SOURCE END ##### ef19192d9219409a9e924a26830026f5\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainformonitor\/mainformonitor_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is Display progress, pause or stop a time-consuming loop by Rafael Oliveira.\r\n      \r\n   \r\n   Background\r\n   It is not uncommon to see questions such asking \"How... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/10\/24\/stop\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,16,28],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5589"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5589"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5589\/revisions"}],"predecessor-version":[{"id":5594,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5589\/revisions\/5594"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}