{"id":263,"date":"2011-01-27T14:39:47","date_gmt":"2011-01-27T14:39:47","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/01\/27\/a-clear-conundrum\/"},"modified":"2016-08-04T09:22:49","modified_gmt":"2016-08-04T14:22:49","slug":"a-clear-conundrum","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/01\/27\/a-clear-conundrum\/","title":{"rendered":"A Clear Conundrum"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Ever been in the situation where you accidently used a variable named something else in MATLAB and it caused you a problem?\r\n         Ever done it with a variable named <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/clear.html\"><tt>clear<\/tt><\/a>?  It can be devilish to get out of the mess. Here are some ways.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#3\">Attempt #1<\/a><\/li>\r\n         <li><a href=\"#4\">Attempt #2<\/a><\/li>\r\n         <li><a href=\"#5\">Attempt #3<\/a><\/li>\r\n         <li><a href=\"#6\">Attempt #4<\/a><\/li>\r\n         <li><a href=\"#7\">What to Try Now?<\/a><\/li>\r\n         <li><a href=\"#10\">More Ways?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>First let's set up the situation.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clear = <span style=\"color: #A020F0\">'mud'<\/span>\r\nzerosAndOnes = [true true false]<\/pre><pre style=\"font-style:oblique\">clear =\r\nmud\r\nzerosAndOnes =\r\n     1     1     0\r\n<\/pre><p>Now we realize we don't need <tt>zerosAndOnes<\/tt>.  Here's what happens if I try to clear that variable.\r\n   <\/p>\r\n   <h3>Attempt #1<a name=\"3\"><\/a><\/h3><pre> clear zerosAndOnes\r\n ??? Error: \"clear\" was previously used as a variable,\r\n  conflicting with its use here as the name of a function or command.\r\n  See MATLAB Programming, \"How MATLAB Recognizes Function Calls That Use Command Syntax\"\r\n  for details.<\/pre><h3>Attempt #2<a name=\"4\"><\/a><\/h3><pre> clear('zerosAndOnes')\r\n ??? Error: \"clear\" was previously used as a variable,\r\n  conflicting with its use here as the name of a function or command.\r\n  See MATLAB Programming, \"How MATLAB Recognizes Function Calls That Use Command Syntax\"\r\n  for details.<\/pre><h3>Attempt #3<a name=\"5\"><\/a><\/h3><pre> clear(zerosAndOnes)\r\n ans =\r\n mu<\/pre><h3>Attempt #4<a name=\"6\"><\/a><\/h3><pre> clear variables\r\n ??? Error: \"clear\" was previously used as a variable,\r\n  conflicting with its use here as the name of a function or command.\r\n  See MATLAB Programming, \"How MATLAB Recognizes Function Calls That Use Command Syntax\"\r\n  for details.<\/pre><h3>What to Try Now?<a name=\"7\"><\/a><\/h3>\r\n   <p>Here are some thoughts on what to try next (and behold, they each work).<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Open the Workspace browser, select the variable named <tt>zerosAndOnes<\/tt> and delete it with the icon containing the red X, or right click on the variable and select \"Delete\"\r\n         <\/li>\r\n         <li>Use the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/builtin.html\"><tt>builtin<\/tt><\/a> to call the built-in version of <tt>clear<\/tt>.\r\n         <\/li>\r\n         <li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/quit.html\"><tt>quit<\/tt><\/a> can help you out, if you are willing and able to start your MATLAB session over.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">who<\/pre><pre style=\"font-style:oblique\">\r\nYour variables are:\r\n\r\nclear         myThoughts    zerosAndOnes  \r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">builtin(<span style=\"color: #A020F0\">'clear'<\/span>,<span style=\"color: #A020F0\">'zerosAndOnes'<\/span>)\r\nwho<\/pre><pre style=\"font-style:oblique\">\r\nYour variables are:\r\n\r\nclear       myThoughts  \r\n\r\n<\/pre><p>Of course, what I <b>should<\/b> do here is <tt>clear<\/tt> the variable named <tt>clear<\/tt> itself now and name it something else.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">builtin(<span style=\"color: #A020F0\">'clear'<\/span>,<span style=\"color: #A020F0\">'clear'<\/span>)\r\nmyThoughts = <span style=\"color: #A020F0\">'mud'<\/span>\r\nwho<\/pre><pre style=\"font-style:oblique\">myThoughts =\r\nmud\r\n\r\nYour variables are:\r\n\r\nmyThoughts  \r\n\r\n<\/pre><h3>More Ways?<a name=\"10\"><\/a><\/h3>\r\n   <p>What have you used to get out of such a snarl in MATLAB?  Did you know about the function <tt>builtin<\/tt> (and that it can't be used with MATLAB code defined in text files, but truly only for ones that are compiled into the MATLAB\r\n      executable)? \r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_740ea83428574eac93e9337bd514d993() {\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='740ea83428574eac93e9337bd514d993 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 740ea83428574eac93e9337bd514d993';\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 2011 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_740ea83428574eac93e9337bd514d993()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\n740ea83428574eac93e9337bd514d993 ##### SOURCE BEGIN #####\r\n%% A Clear Conundrum\r\n% Ever been in the situation where you accidently used a variable named\r\n% something else in MATLAB and it caused you a problem?  Ever done it with\r\n% a variable named\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/clear.html\r\n% |clear|>?  It can be devilish to get out of the mess. Here are some ways.\r\n%%\r\n% First let's set up the situation.\r\nclear = 'mud'\r\nzerosAndOnes = [true true false]\r\n%%\r\n% Now we realize we don't need |zerosAndOnes|.  Here's what happens if I\r\n% try to clear that variable.\r\n\r\n%% Attempt #1\r\n%\r\n%   clear zerosAndOnes\r\n%   ??? Error: \"clear\" was previously used as a variable,\r\n%    conflicting with its use here as the name of a function or command.\r\n%    See MATLAB Programming, \"How MATLAB Recognizes Function Calls That Use Command Syntax\"\r\n%    for details.\r\n%\r\n\r\n%% Attempt #2\r\n%\r\n%   clear('zerosAndOnes')\r\n%   ??? Error: \"clear\" was previously used as a variable,\r\n%    conflicting with its use here as the name of a function or command.\r\n%    See MATLAB Programming, \"How MATLAB Recognizes Function Calls That Use Command Syntax\"\r\n%    for details.\r\n%\r\n\r\n%% Attempt #3\r\n%\r\n%   clear(zerosAndOnes)\r\n%   ans = \r\n%   mu\r\n%\r\n\r\n%% Attempt #4\r\n%\r\n%   clear variables\r\n%   ??? Error: \"clear\" was previously used as a variable,\r\n%    conflicting with its use here as the name of a function or command.\r\n%    See MATLAB Programming, \"How MATLAB Recognizes Function Calls That Use Command Syntax\"\r\n%    for details.\r\n\r\n%% What to Try Now?\r\n% Here are some thoughts on what to try next (and behold, they each work).\r\n%\r\n% * Open the Workspace browser, select the variable named |zerosAndOnes| and\r\n% delete it with the icon containing the red X, or right click on the\r\n% variable and select \"Delete\"\r\n% * Use the function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/builtin.html\r\n% |builtin|> to call the built-in version of |clear|.\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/quit.html\r\n% |quit|> can help you out, if you are willing and able to start your\r\n% MATLAB session over.\r\nwho\r\n%%\r\nbuiltin('clear','zerosAndOnes')\r\nwho\r\n%%\r\n% Of course, what I *should* do here is |clear| the variable named |clear|\r\n% itself now and name it something else.\r\nbuiltin('clear','clear')\r\nmyThoughts = 'mud'\r\nwho\r\n%% More Ways?\r\n% What have you used to get out of such a snarl in MATLAB?  Did you know\r\n% about the function |builtin| (and that it can't be used with MATLAB code\r\n% defined in text files, but truly only for ones that are compiled into the\r\n% MATLAB executable)?  Tell us your stories\r\n% <https:\/\/blogs.mathworks.com\/?p=263 here>.\r\n\r\n\r\n##### SOURCE END ##### 740ea83428574eac93e9337bd514d993\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Ever been in the situation where you accidently used a variable named something else in MATLAB and it caused you a problem?\r\n         Ever done it with a variable named clear?  It... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/01\/27\/a-clear-conundrum\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,29],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/263"}],"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=263"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":1978,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/263\/revisions\/1978"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}