{"id":134,"date":"2008-04-08T10:49:26","date_gmt":"2008-04-08T15:49:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/04\/08\/clearing-the-air\/"},"modified":"2008-04-24T06:52:13","modified_gmt":"2008-04-24T11:52:13","slug":"clearing-the-air","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/04\/08\/clearing-the-air\/","title":{"rendered":"Clearing the Air"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>We have heard repeatedly over the years that users would like a tool to help clean up the workspace by choosing selected variables\r\n         to retain. The function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/clear.html\"><tt>clear<\/tt><\/a>, has logic allowing you to <tt>clear<\/tt> a selection of variables readily, but it's a bit harder to retain a collection using <tt>clear<\/tt>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">What Does clear Clear?<\/a><\/li>\r\n         <li><a href=\"#2\">Debug Gotcha<\/a><\/li>\r\n         <li><a href=\"#3\">How to Keep Variable Selection<\/a><\/li>\r\n         <li><a href=\"#6\">Your Clear Ideas?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>What Does clear Clear?<a name=\"1\"><\/a><\/h3>\r\n   <p>The function <tt>clear<\/tt>, with the <tt>all<\/tt> qualifier, can clear just about everything in MATLAB: variables, functions, MEX-files, breakpoints, Java packages import\r\n      list (from the command prompt).  To additionally clear classes, use <tt>clear classes<\/tt>.\r\n   <\/p>\r\n   <h3>Debug Gotcha<a name=\"2\"><\/a><\/h3>\r\n   <p><tt>clear all<\/tt> clears all breakpoints as well as the workspace, and everything else that isn't nailed down (or perhaps <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mlock.html\"><tt>mlocked<\/tt><\/a> ). This is a frequent source of confusion for people when they are trying to debug code.  If the code being debugged issues\r\n      a <tt>clear<\/tt> statement, the breakpoints get cleared and you could be left with a temporary mystery until you chase down the code doing\r\n      the clearing.\r\n   <\/p>\r\n   <h3>How to Keep Variable Selection<a name=\"3\"><\/a><\/h3>\r\n   <div>\r\n      <ul>\r\n         <li>Use <tt>clear<\/tt> with <tt>-regexp<\/tt> to remove variables selected from memory.\r\n         <\/li>\r\n         <li>Use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/clearvars.html\"><tt>clearvars -except<\/tt><\/a> to retain specific variables in the workspace.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Here's an example of clearing all the variables except those that start with <tt>q<\/tt> (the 17th letter of the English alphabet).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a1 = <span style=\"color: #A020F0\">'Alice'<\/span>;\r\na2 = <span style=\"color: #A020F0\">'Ashley'<\/span>;\r\na3 = <span style=\"color: #A020F0\">'Alistair'<\/span>;\r\nb1 = <span style=\"color: #A020F0\">'Barbara'<\/span>;\r\nb2 = <span style=\"color: #A020F0\">'Brian'<\/span>;\r\ni1 = <span style=\"color: #A020F0\">'Isobel'<\/span>;\r\nl1 = <span style=\"color: #A020F0\">'Loren'<\/span>;\r\nq1 = <span style=\"color: #A020F0\">'Quentin'<\/span>;\r\nq2 = <span style=\"color: #A020F0\">'Quillan'<\/span>;\r\nr1 = <span style=\"color: #A020F0\">'Rosie'<\/span>;\r\nr2 = <span style=\"color: #A020F0\">'Rob'<\/span>;\r\nwho<\/pre><pre style=\"font-style:oblique\">\r\nYour variables are:\r\n\r\nFs        a2        data      ind       q2        ywav      \r\nFswav     a3        datadiff  l1        r1        \r\nN         b1        eqFreqs   matfiles  r2        \r\na1        b2        i1        q1        y         \r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clearvars <span style=\"color: #A020F0\">-except<\/span> <span style=\"color: #A020F0\">q*<\/span>\r\nwho<\/pre><pre style=\"font-style:oblique\">\r\nYour variables are:\r\n\r\nq1  q2  \r\n\r\n<\/pre><h3>Your Clear Ideas?<a name=\"6\"><\/a><\/h3>\r\n   <p>Got any clear ideas?  I'd love to hear them; you can post them <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=134#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f646fee9f853464ab8b4064db78a4d26() {\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='f646fee9f853464ab8b4064db78a4d26 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f646fee9f853464ab8b4064db78a4d26';\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 2008 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_f646fee9f853464ab8b4064db78a4d26()\"><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.6<br><\/p>\r\n<\/div>\r\n<!--\r\nf646fee9f853464ab8b4064db78a4d26 ##### SOURCE BEGIN #####\r\n%% Clearing the Air\r\n% We have heard repeatedly over the years that users would like a tool to\r\n% help clean up the workspace by choosing selected variables to retain. The\r\n% function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/clear.html |clear|>,\r\n% has logic allowing you to |clear| a selection of variables readily, but\r\n% it's a bit harder to retain a collection using |clear|.\r\n%% What Does clear Clear?\r\n% The function |clear|, with the |all| qualifier, can clear just about \r\n% everything in MATLAB: variables, functions, MEX-files, breakpoints, Java\r\n% packages import list (from the command prompt).  To additionally clear\r\n% classes, use |clear classes|.\r\n\r\n%% Debug Gotcha\r\n% |clear all| clears all breakpoints as well as the workspace, and\r\n% everything else that isn't nailed down (or perhaps \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/mlock.html |mlocked|> ).\r\n% This is a frequent source of confusion for people when they are trying to\r\n% debug code.  If the code being debugged issues a |clear| statement, the\r\n% breakpoints get cleared and you could be left with a temporary mystery\r\n% until you chase down the code doing the clearing.\r\n%% How to Keep Variable Selection\r\n% \r\n% * Use |clear| with |-regexp| to remove variables selected from memory.\r\n% * Use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/clearvars.html |clearvars -except|>\r\n% to retain specific variables in the workspace.\r\n%% \r\n% Here's an example of clearing all the variables except those that start\r\n% with |q| (the 17th letter of the English alphabet).\r\na1 = 'Alice';\r\na2 = 'Ashley';\r\na3 = 'Alistair';\r\nb1 = 'Barbara';\r\nb2 = 'Brian';\r\ni1 = 'Isobel';\r\nl1 = 'Loren';\r\nq1 = 'Quentin';\r\nq2 = 'Quillan';\r\nr1 = 'Rosie';\r\nr2 = 'Rob';\r\nwho\r\n%%\r\nclearvars -except q*\r\nwho\r\n%% Your Clear Ideas?\r\n% Got any clear ideas?  I'd love to hear them; you can post them\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=134#respond here>.\r\n##### SOURCE END ##### f646fee9f853464ab8b4064db78a4d26\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      We have heard repeatedly over the years that users would like a tool to help clean up the workspace by choosing selected variables\r\n         to retain. The function clear, has logic... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/04\/08\/clearing-the-air\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/134"}],"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=134"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}