{"id":1702,"date":"2016-07-11T10:18:12","date_gmt":"2016-07-11T15:18:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1702"},"modified":"2016-06-22T10:22:22","modified_gmt":"2016-06-22T15:22:22","slug":"clearing-the-air-in-your-matlab-session","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2016\/07\/11\/clearing-the-air-in-your-matlab-session\/","title":{"rendered":"Clearing the Air in Your MATLAB Session"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>I have, over time, changed when, where, and how I \"clean up\" my MATLAB session.  You may want to update your strategy as well.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#855b96fa-99f8-4fa9-96a3-b4ae1aaa7613\">What I Used to Do<\/a><\/li><li><a href=\"#69164626-c3d1-45ed-b485-aafbcc2f279b\">What I Do Now<\/a><\/li><li><a href=\"#2c80110d-4543-45b8-87d9-c09a01429890\">How Do I Run the Code?<\/a><\/li><li><a href=\"#5b8df842-e2da-4631-85d4-08e30e0b7f96\">What Do You Do?<\/a><\/li><\/ul><\/div><h4>What I Used to Do<a name=\"855b96fa-99f8-4fa9-96a3-b4ae1aaa7613\"><\/a><\/h4><p>In the recent past, if I wanted to \"start from scratch\" without restarting MATLAB, I would clear my workspace, command window, and close open figures.  I would issue the following commands<\/p><pre>         clc\r\n         clear all\r\n         close all force<\/pre><h4>What I Do Now<a name=\"69164626-c3d1-45ed-b485-aafbcc2f279b\"><\/a><\/h4><p>I still do something very similar, but with an important though small-looking change.  I remove the <tt>all<\/tt> from my <tt>clear<\/tt> statement. Here's what I do now.<\/p><pre>         clc\r\n         clear\r\n         close all force<\/pre><p>I no longer ask for everything to be cleared. Why not?  Because it is overkill, and I am potentially clearing out things I would prefer to keep.  Such as?, you might say.  See <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/clear.html#inputarg_ItemType\"><tt>this table<\/tt><\/a>, also copied here, to see the items.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/clearDocTable.png\" alt=\"\"> <\/p><p>You'll notice that the old <tt>clear all<\/tt> clears out much more than just variables in my workspace.  In particular, it clears out functions and MEX functions that I've already run.  There is no good reason to clear these out.  In fact, you pay a price (perhaps small, perhaps not so small) when you run a function for the first time, since MATLAB needs to find the function (assuming it's MATLAB or MEX code) on a disk in a location on your <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/path.html\">MATLAB <tt>path<\/tt><\/a>, read it in, and prepare it so it can run (e.g., parse). Once MATLAB processes the file once, it does not need to do so again as long as the code remains in your session.<\/p><h4>How Do I Run the Code?<a name=\"2c80110d-4543-45b8-87d9-c09a01429890\"><\/a><\/h4><p>Actually, instead of typing 3 lines, or even creating a code file, I use a MATLAB shortcut on my Quick Access toolbar.  Here's what my entry looks like.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/cccShortcut.png\" alt=\"\"> <\/p><p>You'll notice that I use \"ccc\" as the tooltip for when my mouse hovers over the label.  And the label itself is \"X\".<\/p><h4>What Do You Do?<a name=\"5b8df842-e2da-4631-85d4-08e30e0b7f96\"><\/a><\/h4><p>What do you do when you want to reset your MATLAB session?  Start over? Reset the random number generator?  I'd love to know your thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=1702#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_5bcf38d4c944448baf6e16f0c77dd647() {\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='5bcf38d4c944448baf6e16f0c77dd647 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5bcf38d4c944448baf6e16f0c77dd647';\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        copyright = 'Copyright 2016 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5bcf38d4c944448baf6e16f0c77dd647()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\n5bcf38d4c944448baf6e16f0c77dd647 ##### SOURCE BEGIN #####\r\n%% Clearing the Air in Your MATLAB Session\r\n% I have, over time, changed when, where, and how I \"clean up\" my MATLAB\r\n% session.  You may want to update your strategy as well.\r\n%% What I Used to Do\r\n% In the recent past, if I wanted to \"start from scratch\" without\r\n% restarting MATLAB, I would clear my workspace, command window, and close\r\n% open figures.  I would issue the following commands\r\n%\r\n%           clc\r\n%           clear all\r\n%           close all force\r\n%\r\n%% What I Do Now\r\n% I still do something very similar, but with an important though\r\n% small-looking change.  I remove the |all| from my |clear| statement.\r\n% Here's what I do now.\r\n%\r\n%           clc\r\n%           clear\r\n%           close all force\r\n%%\r\n% I no longer ask for everything to be cleared. Why not?  Because it is\r\n% overkill, and I am potentially clearing out things I would prefer to\r\n% keep.  Such as?, you might say.  See\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/clear.html#inputarg_ItemType\r\n% |this table|>, also copied here, to see the items.\r\n% \r\n% <<clearDocTable.png>>\r\n% \r\n%\r\n%%\r\n% You'll notice that the old |clear all| clears out much more than just\r\n% variables in my workspace.  In particular, it clears out functions and\r\n% MEX functions that I've already run.  There is no good reason to clear\r\n% these out.  In fact, you pay a price (perhaps small, perhaps not so\r\n% small) when you run a function for the first time, since MATLAB needs to\r\n% find the function (assuming it's MATLAB or MEX code) on a disk in a\r\n% location on your <https:\/\/www.mathworks.com\/help\/matlab\/ref\/path.html\r\n% MATLAB |path|>, read it in, and prepare it so it can run (e.g., parse).\r\n% Once MATLAB processes the file once, it does not need to do so again as\r\n% long as the code remains in your session.\r\n%% How Do I Run the Code?\r\n% Actually, instead of typing 3 lines, or even creating a code file, I use\r\n% a MATLAB shortcut on my Quick Access toolbar.  Here's what my entry\r\n% looks like.\r\n%\r\n% <<cccShortcut.png>>\r\n%\r\n% You'll notice that I use \"ccc\" as the tooltip for when my mouse hovers\r\n% over the label.  And the label itself is \"X\".\r\n%% What Do You Do?\r\n% What do you do when you want to reset your MATLAB session?  Start over?\r\n% Reset the random number generator?  I'd love to know your thoughts\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=1702#respond here>.\r\n\r\n##### SOURCE END ##### 5bcf38d4c944448baf6e16f0c77dd647\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2016\/clearDocTable.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>I have, over time, changed when, where, and how I \"clean up\" my MATLAB session.  You may want to update your strategy as well.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2016\/07\/11\/clearing-the-air-in-your-matlab-session\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1702"}],"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=1702"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1702\/revisions"}],"predecessor-version":[{"id":1704,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1702\/revisions\/1704"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}