{"id":80,"date":"2007-03-07T12:28:16","date_gmt":"2007-03-07T17:28:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=80"},"modified":"2018-03-03T10:33:15","modified_gmt":"2018-03-03T15:33:15","slug":"more-matlab-gotchas","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/03\/07\/more-matlab-gotchas\/","title":{"rendered":"More MATLAB Gotchas"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I recently wrote an article on <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=78\">Beginner Woes<\/a>, and I thought I would add some more gotcha topics today.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Clearing Everything<\/a><\/li>\r\n         <li><a href=\"#3\">Working with mlock'ed Files<\/a><\/li>\r\n         <li><a href=\"#4\">Variable Names Worth Avoiding<\/a><\/li>\r\n         <li><a href=\"#6\">Clearing Axes<\/a><\/li>\r\n         <li><a href=\"#11\">Know of Other Gotchas?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Clearing Everything<a name=\"1\"><\/a><\/h3>\r\n   <p>I've seen people in the middle of a session type <tt>clear all<\/tt> and then try to \"continue\" debugging.  Even more insidious is when <tt>clear all<\/tt> is buried in some code that the program being debugged calls. The problem is that <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/clear.html\"><tt>clear all<\/tt><\/a> clears more than just the workspace. To quote from the reference page:\r\n   <\/p>\r\n   <p><i>Removes all variables, functions, and MEX-files from memory, leaving the workspace empty. Using clear all removes debugging\r\n         breakpoints in M-files and reinitializes persistent variables, since the breakpoints for a function and persistent variables\r\n         are cleared whenever the M-file is changed or cleared. When issued from the Command Window prompt, also removes the Java packages\r\n         import list.<\/i><\/p>\r\n   <p>To clear just variables, use <tt>clear variables<\/tt> instead.  <tt>clear all<\/tt> is a very heavyweight tool for some chores.\r\n   <\/p>\r\n   <h3>Working with mlock'ed Files<a name=\"3\"><\/a><\/h3>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mlock.html\"><tt>mlock<\/tt><\/a> allows you to depend on M-file and MEX-files not being cleared from memory.  This, in turn, allows you to be sure that <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/persistent.html\"><tt>persistent<\/tt><\/a> variables stay in memory.  However, when you are editing a file that uses <tt>mlock<\/tt>, and you are unaware that it is locked, you may be mystified why MATLAB is not recognizing your file changes.  In this case,\r\n      you must use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/munlock.html\"><tt>munlock<\/tt><\/a>, <tt>clear<\/tt> the file, and then run the desired code again.  The function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mislocked.html\"><tt>mislocked<\/tt><\/a> can tell you that status of the file in question.\r\n   <\/p>\r\n   <h3>Variable Names Worth Avoiding<a name=\"4\"><\/a><\/h3>\r\n   <p>Many people have commented on MATLAB's flat namespace.  This is sometimes a positive and sometimes a negative.  If you name\r\n      a variable the same name as a MATLAB function, the variable shadows the function.  We have seen several posts to the MATLAB newsgroup questioning why the user gets an error trying to put a title on a plot, for example.  The usual reason is that the user has\r\n      a variable named \"title\" that interferes with the function.  Here are two of the possible error messages. (They are hard to\r\n      reproduce in the published M-file.)\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">emsg1 = sprintf([<span style=\"color: #A020F0\">'Error: \"cd\" was previously used as a variable,\\n'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n <span style=\"color: #A020F0\">'conflicting with its use here as the name of a function or command.\\n '<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n <span style=\"color: #A020F0\">'See MATLAB Programming, \"Determining Which Function Is Called\" for details.'<\/span>])\r\nemsg2 = <span style=\"color: #A020F0\">'Index exceeds matrix dimensions.'<\/span><\/pre><pre style=\"font-style:oblique\">emsg1 =\r\nError: \"cd\" was previously used as a variable,\r\nconflicting with its use here as the name of a function or command.\r\n See MATLAB Programming, \"Determining Which Function Is Called\" for details.\r\nemsg2 =\r\nIndex exceeds matrix dimensions.\r\n<\/pre><p>Typical \"CULPRITS\"  - variable names that often cause problems:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><tt>cd<\/tt><\/li>\r\n         <li><tt>length<\/tt><\/li>\r\n         <li><tt>size<\/tt><\/li>\r\n         <li><tt>title<\/tt><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Clearing Axes<a name=\"6\"><\/a><\/h3>\r\n   <p>Recently there was a post on the MATLAB newsgroup about a \"bug\" in clearing the axes after a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/comet.html\"><tt>comet<\/tt><\/a> plot. When you type\r\n   <\/p><pre>       comet(x,y)<\/pre><p>you see this plot<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">t = 0:pi\/40:2*pi;\r\nx = -sin(4*t);\r\ny = cos(2*t);\r\ncometplot<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/80\/gotchas_01.png\"> <p>The user reasonably expected the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cla.html\"><tt>cla<\/tt><\/a> command to clear the axes.  <tt>cla<\/tt> left the full line in the axes, as in the previous plot.\r\n   <\/p>\r\n   <p>However, <tt>comet<\/tt> does not use the default mode for erasing lines so it can leave a trail. So here's what you see on the screen after issuing <tt>cla<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cometplotcla<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/80\/gotchas_02.png\"> <p>Because of the non-default erasemode, issuing <tt>cla reset<\/tt> is in order instead of the default <tt>cla<\/tt>.  Here's what you see then.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cometplotreset<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/80\/gotchas_03.png\"> <h3>Know of Other Gotchas?<a name=\"11\"><\/a><\/h3>\r\n   <p>Do you happen to have other gotchas that are worth sharing?  Post them <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=80#respond\">here<\/a> please.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_e1149902cdd24764bf2d93c464ff1ada() {\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='e1149902cdd24764bf2d93c464ff1ada ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e1149902cdd24764bf2d93c464ff1ada';\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_e1149902cdd24764bf2d93c464ff1ada()\"><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\ne1149902cdd24764bf2d93c464ff1ada ##### SOURCE BEGIN #####\r\n%% More MATLAB Gotchas\r\n% I recently wrote an article on <https:\/\/blogs.mathworks.com\/loren\/?p=78 Beginner Woes>,\r\n% and I thought I would add some more gotcha topics today.\r\n%% Clearing Everything\r\n% I've seen people in the middle of a session type |clear all| and then try\r\n% to \"continue\" debugging.  Even more insidious is when |clear all| is\r\n% buried in some code that the program being debugged calls. The problem is\r\n% that\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/clear.html |clear all|>\r\n% clears more than just the workspace. To quote from the reference page:\r\n%\r\n% _Removes all variables, functions, and MEX-files from memory, leaving the\r\n% workspace empty. Using clear all removes debugging breakpoints in M-files\r\n% and reinitializes persistent variables, since the breakpoints for a\r\n% function and persistent variables are cleared whenever the M-file is\r\n% changed or cleared. When issued from the Command Window prompt, also\r\n% removes the Java packages import list._\r\n%\r\n%%\r\n% To clear just variables, use |clear variables| instead.  |clear all| is a\r\n% very heavyweight tool for some chores.\r\n%% Working with mlock'ed Files\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/mlock.html |mlock|>\r\n% allows you to depend on M-file and MEX-files not being cleared from\r\n% memory.  This, in turn, allows you to be sure that \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/persistent.html |persistent|>\r\n% variables stay in memory.  However, when you are editing a file that uses\r\n% |mlock|, and you are unaware that it is locked, you may be mystified why\r\n% MATLAB is not recognizing your file changes.  In this case, you must use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/munlock.html |munlock|>,\r\n% |clear| the file, and then run the desired code again.  The function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/mislocked.html |mislocked|>\r\n% can tell you that status of the file in question.\r\n%%  Variable Names Worth Avoiding\r\n% Many people have commented on MATLAB's flat namespace.  This is sometimes\r\n% a positive and sometimes a negative.  If you name a variable the same\r\n% name as a MATLAB function, the variable shadows the function.  We have\r\n% seen several posts to the <http:\/\/newsreader.mathworks.com MATLAB newsgroup>\r\n% questioning why the user gets an error trying to put a title on a plot,\r\n% for example.  The usual reason is that the user has a variable named\r\n% \"title\" that interferes with the function.  Here are two of the possible\r\n% error messages. (They are hard to reproduce in the published M-file.)\r\nemsg1 = sprintf(['Error: \"cd\" was previously used as a variable,\\n', ...\r\n 'conflicting with its use here as the name of a function or command.\\n ', ...\r\n 'See MATLAB Programming, \"Determining Which Function Is Called\" for details.'])\r\nemsg2 = 'Index exceeds matrix dimensions.'\r\n%%\r\n% Typical \"CULPRITS\"  - variable names that often cause problems:\r\n%\r\n% * |cd|\r\n% * |length|\r\n% * |size|\r\n% * |title|\r\n\r\n%% Clearing Axes\r\n% Recently there was a post on the MATLAB newsgroup about a \"bug\" in\r\n% clearing the axes after a  \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/comet.html |comet|>\r\n% plot.\r\n% When you type\r\n%\r\n%         comet(x,y)\r\n%%\r\n% you see this plot\r\nt = 0:pi\/40:2*pi;\r\nx = -sin(4*t);\r\ny = cos(2*t);\r\ncometplot\r\n%%\r\n% The user reasonably expected the \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/cla.html |cla|>\r\n% command to clear the axes.  |cla| left the full line in the axes, as in\r\n% the previous plot.\r\n%%\r\n% However, |comet| does not use the default\r\n% mode for erasing lines so it can leave a trail.  \r\n% (For more on erasemodes, look\r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/creating_plots\/f10-1497.html here>.)\r\n% So here's what you see on the screen after issuing |cla|.\r\ncometplotcla\r\n%%\r\n% Because of the non-default erasemode, issuing |cla reset| is in order\r\n% instead of the default |cla|.  Here's what you see then.\r\ncometplotreset\r\n%% Know of Other Gotchas?\r\n% Do you happen to have other gotchas that are worth sharing?  Post them\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=80#respond here> please.\r\n##### SOURCE END ##### e1149902cdd24764bf2d93c464ff1ada\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I recently wrote an article on Beginner Woes, and I thought I would add some more gotcha topics today.\r\n      \r\n   \r\n   Contents\r\n   \r\n      \r\n... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/03\/07\/more-matlab-gotchas\/\">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],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/80"}],"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=80"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":2734,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/80\/revisions\/2734"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}