{"id":165,"date":"2008-12-11T18:11:04","date_gmt":"2008-12-11T18:11:04","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/12\/11\/when-matlab-refreshes-directories\/"},"modified":"2009-05-21T13:48:48","modified_gmt":"2009-05-21T13:48:48","slug":"when-matlab-refreshes-directories","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/12\/11\/when-matlab-refreshes-directories\/","title":{"rendered":"When MATLAB Refreshes Directories"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Over the years, there have been lots of questions about when MATLAB recognizes files on the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/path.html\"><tt>path<\/tt><\/a>.  Today I'm going to talk about the case where a user generates or updates MATLAB code, and then wants to use it. What do you\r\n         have to do to be sure MATLAB sees it?  I am specifically talking about user files in user directories and NOT about files\r\n         underneath <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/matlabroot.html\"><tt>matlabroot<\/tt><\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#3\">The Code That Perplexed<\/a><\/li>\r\n         <li><a href=\"#10\">Fixing the Problem<\/a><\/li>\r\n         <li><a href=\"#15\">Why Does This Solution Work?<\/a><\/li>\r\n         <li><a href=\"#16\">Original Function<\/a><\/li>\r\n         <li><a href=\"#19\">Repaired Function<\/a><\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> fcnPuzzle()<\/pre><p>Note:  I am doing an experiment by publishing a function with subfunctions so you can see the code with the benefit of syntax\r\n      highlighting for the code that's not the blog-code itself.  I hope this ends up working and being helpful.\r\n   <\/p>\r\n   <h3>The Code That Perplexed<a name=\"3\"><\/a><\/h3>\r\n   <p>See the function <tt>fcnBug<\/tt> towards the bottom of this post. One puzzle is that if you run the code from the debugger, the results are always the expected\r\n      ones, but not when running the usual way. I'll illustrate the conundrum by running it a few different ways.  Also see the\r\n      repaired version <tt>fcnBugRepair<\/tt> following the buggy version.\r\n   <\/p>\r\n   <p>Show the file doesn't exist.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">exist(<span style=\"color: #A020F0\">'fcnTest.m'<\/span>,<span style=\"color: #A020F0\">'file'<\/span>)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0\r\n<\/pre><p>Small puzzle.  If I don't put my first attempt, before the file exists, into a <tt>try-catch<\/tt>, my function fails with an error.  However, if I place the same code inside a <tt>try-catch<\/tt>, it seems to not generate the error.  Let's ignore that mystery for today.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    fcnBug( <span style=\"color: #A020F0\">'C'<\/span>, <span style=\"color: #A020F0\">'clear'<\/span>)\r\n<span style=\"color: #0000FF\">catch<\/span> ignoreException\r\n    disp(<span style=\"color: #A020F0\">'error when file doesn''t exist before running'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>Only the original function written out is called, not the updated versions, assuming we do nothing else in the function to\r\n      affect the workspace.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">  <span style=\"color: #0000FF\">for<\/span> d=1:4\r\n     fcnBug( num2str(d), <span style=\"color: #A020F0\">'nothing'<\/span>)\r\n  <span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">1 Problem\r\n1 Problem\r\n1 Problem\r\n1 Problem\r\n<\/pre><p>Clearing the function before calling it helps.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">  <span style=\"color: #0000FF\">for<\/span> d=1:4\r\n     fcnBug( num2str(d), <span style=\"color: #A020F0\">'clear'<\/span>)\r\n  <span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">1 Problem\r\n2 Problem\r\n3 Problem\r\n4 Problem\r\n<\/pre><p>Another way to influence MATLAB to see the new function is to ask if the file <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/exist.html\"><tt>exists<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">  delete <span style=\"color: #A020F0\">fcnTest.m<\/span>\r\n  <span style=\"color: #0000FF\">for<\/span> d=1:4\r\n     fcnBug(num2str(d), <span style=\"color: #A020F0\">'clear+exist'<\/span>)\r\n  <span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">1 Problem\r\n2 Problem\r\n3 Problem\r\n4 Problem\r\n<\/pre><p>Clean up.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">delete <span style=\"color: #A020F0\">fcnTest.m<\/span><\/pre><h3>Fixing the Problem<a name=\"10\"><\/a><\/h3>\r\n   <p>There is an easy solution to being sure that the new file is recognized. Directly after closing the file, force MATLAB to\r\n      clear the one in memory. You can do this with a lighter or heavier hand, depending on your situation.  Simply <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/clear.html\"><tt>clearing<\/tt><\/a> the function is enough, or you might try to <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/rehash.html\"><tt>rehash<\/tt><\/a> the files MATLAB knows about.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">try<\/span>\r\n    fcnBugRepair( <span style=\"color: #A020F0\">'C'<\/span>, <span style=\"color: #A020F0\">'clear'<\/span>)\r\n<span style=\"color: #0000FF\">catch<\/span> ignoreException\r\n    disp(<span style=\"color: #A020F0\">'error when file doesn''t exist before running'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">error when file doesn't exist before running\r\n<\/pre><p>Let me show this working with the repaired function. This was the case earlier in which only the file originally written out\r\n      was called.  You can see here that each version is now executed, even when nothing additional is done (except the addition\r\n      of the <tt>clear<\/tt> statement in the function code).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> d=1:4\r\n   fcnBugRepair( num2str(d), <span style=\"color: #A020F0\">'nothing'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">1 Problem\r\n2 Problem\r\n3 Problem\r\n4 Problem\r\n<\/pre><p>As before, clearing the workspace before execution still has the good effect.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> d=1:4\r\n   fcnBugRepair( num2str(d), <span style=\"color: #A020F0\">'clear'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">1 Problem\r\n2 Problem\r\n3 Problem\r\n4 Problem\r\n<\/pre><p>Again, the test for function existence also works.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">delete( <span style=\"color: #A020F0\">'fcnTest.m'<\/span>);\r\n<span style=\"color: #0000FF\">for<\/span> d=1:4\r\n    fcnBugRepair(num2str(d), <span style=\"color: #A020F0\">'clear+exist'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">1 Problem\r\n2 Problem\r\n3 Problem\r\n4 Problem\r\n<\/pre><p>Clean Up<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">delete <span style=\"color: #A020F0\">fcnTest.m<\/span><\/pre><h3>Why Does This Solution Work?<a name=\"15\"><\/a><\/h3>\r\n   <p>The problem is that of timestamp resolution for the file, which on most filesystems is only 1 second. Since this is being\r\n      run in a tight loop, the file <tt>fcnTest.m<\/tt> is being rewritten during the same clock tick and therefore MATLAB has no way of knowing that it has changed. Therefore,\r\n      MATLAB originally continued to use the cached version.\r\n   <\/p>\r\n   <p><tt>clear<\/tt> forces MATLAB to dump its cache, and the next time MATLAB runs the function it reloads it from disk.\r\n   <\/p>\r\n   <p>The functions <tt>exist<\/tt> and <tt>rehash<\/tt> each cause MATLAB to notice the file change because they each refresh \"dirty\" directories.\r\n   <\/p>\r\n   <p>The reason MATLAB doesn't always look for changes is for performance. <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/fclose.html\"><tt>fclose<\/tt><\/a> does notify the path manager that a certain directory is changed but the cache update won't happen until someone explicitly\r\n      refreshes the directory -- the refreshing can happen in the ways I've already discussed, or at the next command prompt (that\r\n      is why it works in debugger).\r\n   <\/p>\r\n   <h3>Original Function<a name=\"16\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> fcnBug( id, mode)<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% fcnBug - unexpected bug<\/span>\r\n<span style=\"color: #228B22\">%<\/span>\r\n<span style=\"color: #228B22\">% %% Error<\/span>\r\n<span style=\"color: #228B22\">% % when the file does not exist before run<\/span>\r\n<span style=\"color: #228B22\">% fcnBug( 'C', 'clear')<\/span>\r\n<span style=\"color: #228B22\">% %% Only the 'base workspace one'<\/span>\r\n<span style=\"color: #228B22\">% % is called<\/span>\r\n<span style=\"color: #228B22\">% for d=1:4<\/span>\r\n<span style=\"color: #228B22\">%    fcnBug( num2str(d), 'nothing')<\/span>\r\n<span style=\"color: #228B22\">% end<\/span>\r\n<span style=\"color: #228B22\">% %% Clear the workspace<\/span>\r\n<span style=\"color: #228B22\">% % helps<\/span>\r\n<span style=\"color: #228B22\">% for d=1:4<\/span>\r\n<span style=\"color: #228B22\">%    fcnBug( num2str(d), 'clear')<\/span>\r\n<span style=\"color: #228B22\">% end<\/span>\r\n<span style=\"color: #228B22\">% %% Not at creation time<\/span>\r\n<span style=\"color: #228B22\">% delete( 'fcnTest.m');<\/span>\r\n<span style=\"color: #228B22\">% fcnBug( num2str(1), 'clear')<\/span>\r\n<span style=\"color: #228B22\">% %% Exists<\/span>\r\n<span style=\"color: #228B22\">% % seems to tell to MATLAB that a function is here<\/span>\r\n<span style=\"color: #228B22\">% delete( 'fcnTest.m');<\/span>\r\n<span style=\"color: #228B22\">% for d=1:4<\/span>\r\n<span style=\"color: #228B22\">%    fcnBug(num2str(d), 'clear+exist')<\/span>\r\n<span style=\"color: #228B22\">% end<\/span>\r\nfid = fopen(<span style=\"color: #A020F0\">'.\\fcnTest.m'<\/span>, <span style=\"color: #A020F0\">'w'<\/span>);\r\nfprintf(fid, <span style=\"color: #A020F0\">'function fcnTest(varargin)\\ndisp(''%s Problem'')'<\/span>, id);\r\nfclose(fid);\r\n\r\n<span style=\"color: #0000FF\">switch<\/span> lower(mode)\r\n    <span style=\"color: #0000FF\">case<\/span> <span style=\"color: #A020F0\">'clear+exist'<\/span>\r\n        clear <span style=\"color: #A020F0\">fcnTest<\/span>\r\n        exist( <span style=\"color: #A020F0\">'fcnTest.m'<\/span>, <span style=\"color: #A020F0\">'file'<\/span>);\r\n    <span style=\"color: #0000FF\">case<\/span> <span style=\"color: #A020F0\">'clear'<\/span>\r\n        clear <span style=\"color: #A020F0\">fcnTest<\/span>\r\n    <span style=\"color: #0000FF\">case<\/span> <span style=\"color: #A020F0\">'nothing'<\/span>\r\n<span style=\"color: #0000FF\">end<\/span>\r\nfcnTest;<\/pre><pre style=\"font-style:oblique\">error when file doesn't exist before running<\/pre><h3>Repaired Function<a name=\"19\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> fcnBugRepair( id, mode)\r\n<span style=\"color: #228B22\">% Repaired by adding clear statement in a good location<\/span>\r\nfid = fopen(<span style=\"color: #A020F0\">'.\\fcnTest.m'<\/span>, <span style=\"color: #A020F0\">'w'<\/span>);\r\nfprintf(fid, <span style=\"color: #A020F0\">'function fcnTest(varargin)\\ndisp(''%s Problem'')'<\/span>, id);\r\nfclose(fid);\r\n\r\nclear <span style=\"color: #A020F0\">fcnTest<\/span>  <span style=\"color: #228B22\">% or could use rehash here instead<\/span>\r\n\r\n<span style=\"color: #0000FF\">switch<\/span> lower(mode)\r\n    <span style=\"color: #0000FF\">case<\/span> <span style=\"color: #A020F0\">'clear+exist'<\/span>\r\n        clear <span style=\"color: #A020F0\">fcnTest<\/span>\r\n        exist( <span style=\"color: #A020F0\">'fcnTest.m'<\/span>, <span style=\"color: #A020F0\">'file'<\/span>);\r\n    <span style=\"color: #0000FF\">case<\/span> <span style=\"color: #A020F0\">'clear'<\/span>\r\n        clear <span style=\"color: #A020F0\">fcnTest<\/span>\r\n    <span style=\"color: #0000FF\">case<\/span> <span style=\"color: #A020F0\">'nothing'<\/span>\r\n<span style=\"color: #0000FF\">end<\/span>\r\nfcnTest;<\/pre><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a4ae532911b8407dbe9a4ce94551978f() {\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='a4ae532911b8407dbe9a4ce94551978f ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a4ae532911b8407dbe9a4ce94551978f';\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_a4ae532911b8407dbe9a4ce94551978f()\"><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.7<br><\/p>\r\n<\/div>\r\n<!--\r\na4ae532911b8407dbe9a4ce94551978f ##### SOURCE BEGIN #####\r\n%% When MATLAB Refreshes Directories\r\n% Over the years, there have been lots of questions about when MATLAB\r\n% recognizes files on the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/path.html\r\n% |path|>.  Today I'm going to talk about the case where a user generates\r\n% or updates MATLAB code, and then wants to use it. What do you have to do to\r\n% be sure MATLAB sees it?  I am specifically talking about user files in\r\n% user directories and NOT about files underneath\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/matlabroot.html |matlabroot|>.\r\n\r\n%% \r\nfunction fcnPuzzle()\r\n%%\r\n% Note:  I am doing an experiment by publishing a function with\r\n% subfunctions so you can see the code with the benefit of syntax\r\n% highlighting for the code that's not the blog-code itself.  I hope this\r\n% ends up working and being helpful.\r\n%% The Code That Perplexed\r\n% See the function |fcnBug| towards the bottom of this post.\r\n% One puzzle is that if you run the code from the debugger, the results\r\n% are always the expected ones, but not when running the usual way.\r\n% I'll illustrate the conundrum by running it a few different ways.  Also\r\n% see the repaired version |fcnBugRepair| following the buggy version.\r\n%%\r\n% Show the file doesn't exist.\r\nexist('fcnTest.m','file')\r\n\r\n%% \r\n% Small puzzle.  If I don't put my first attempt, before the file exists,\r\n% into a |try-catch|, my function fails with an error.  However, if I\r\n% place the same code inside a |try-catch|, it seems to not generate the\r\n% error.  Let's ignore that mystery for today.\r\n\r\ntry\r\n    fcnBug( 'C', 'clear')\r\ncatch ignoreException\r\n    disp('error when file doesn''t exist before running')\r\nend\r\n%%\r\n% Only the original function written out is called, not the updated\r\n% versions, assuming we do nothing else in the function to affect the\r\n% workspace.\r\n  for d=1:4\r\n     fcnBug( num2str(d), 'nothing')\r\n  end\r\n%%\r\n% Clearing the function before calling it helps.\r\n  for d=1:4\r\n     fcnBug( num2str(d), 'clear')\r\n  end\r\n%% \r\n% Another way to influence MATLAB to see the new function is to ask if the\r\n% file <https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/exist.html |exists|>.\r\n  delete fcnTest.m\r\n  for d=1:4\r\n     fcnBug(num2str(d), 'clear+exist')\r\n  end\r\n%% \r\n% Clean up.\r\ndelete fcnTest.m\r\n%% Fixing the Problem\r\n% There is an easy solution to being sure that the new file is recognized.\r\n% Directly after closing the file, force MATLAB to clear the one in memory.\r\n% You can do this with a lighter or heavier hand, depending on your\r\n% situation.  Simply\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/clear.html\r\n% |clearing|> the function is enough, or you might try to\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/rehash.html |rehash|> \r\n% the files MATLAB knows about.\r\ntry\r\n    fcnBugRepair( 'C', 'clear')\r\ncatch ignoreException\r\n    disp('error when file doesn''t exist before running')\r\nend\r\n%% \r\n% Let me show this working with the repaired function. This was the case\r\n% earlier in which only the file originally written out was called.  You\r\n% can see here that each version is now executed, even when nothing\r\n% additional is done (except the addition of the |clear| statement in the\r\n% function code).\r\nfor d=1:4\r\n   fcnBugRepair( num2str(d), 'nothing')\r\nend\r\n%%\r\n% As before, clearing the workspace before execution still has the good\r\n% effect.\r\nfor d=1:4\r\n   fcnBugRepair( num2str(d), 'clear')\r\nend\r\n\r\n%%\r\n% Again, the test for function existence also works.\r\ndelete( 'fcnTest.m');\r\nfor d=1:4\r\n    fcnBugRepair(num2str(d), 'clear+exist')\r\nend\r\n  \r\n%%\r\n% Clean Up\r\ndelete fcnTest.m  \r\n%% Why Does This Solution Work?\r\n% The problem is that of timestamp resolution for the file, which on most\r\n% filesystems is only 1 second. Since this is being run in a tight\r\n% loop, the file |fcnTest.m| is being rewritten during the same\r\n% clock tick and therefore MATLAB has no way of knowing that it has\r\n% changed. Therefore, MATLAB originally continued to use the cached version.\r\n% \r\n% |clear| forces MATLAB to dump its cache, and the next time MATLAB runs\r\n% the function it reloads it from disk.\r\n% \r\n% The functions |exist| and |rehash| each cause MATLAB to notice \r\n% the file change because they each refresh \"dirty\" directories.\r\n% \r\n% The reason MATLAB doesn't always look for changes is for performance.  \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/fclose.html |fclose|>\r\n% does notify the path manager that a certain directory is changed but the\r\n% cache update won't happen until someone explicitly refreshes the\r\n% directory REPLACE_WITH_DASH_DASH the refreshing can happen in the ways I've already\r\n% discussed, or at the next command prompt (that is why it works in debugger).\r\n\r\n\r\n%% Original Function\r\nfunction fcnBug( id, mode)\r\n% fcnBug - unexpected bug\r\n%\r\n% %% Error\r\n% % when the file does not exist before run\r\n% fcnBug( 'C', 'clear')\r\n% %% Only the 'base workspace one'\r\n% % is called\r\n% for d=1:4\r\n%    fcnBug( num2str(d), 'nothing')\r\n% end\r\n% %% Clear the workspace\r\n% % helps\r\n% for d=1:4\r\n%    fcnBug( num2str(d), 'clear')\r\n% end\r\n% %% Not at creation time\r\n% delete( 'fcnTest.m');\r\n% fcnBug( num2str(1), 'clear')\r\n% %% Exists\r\n% % seems to tell to MATLAB that a function is here\r\n% delete( 'fcnTest.m');\r\n% for d=1:4\r\n%    fcnBug(num2str(d), 'clear+exist')\r\n% end\r\nfid = fopen('.\\fcnTest.m', 'w');\r\nfprintf(fid, 'function fcnTest(varargin)\\ndisp(''%s Problem'')', id);\r\nfclose(fid);\r\n\r\nswitch lower(mode)\r\n    case 'clear+exist'\r\n        clear fcnTest\r\n        exist( 'fcnTest.m', 'file');\r\n    case 'clear'\r\n        clear fcnTest\r\n    case 'nothing'\r\nend\r\nfcnTest;\r\n%% Repaired Function\r\nfunction fcnBugRepair( id, mode)\r\n% Repaired by adding clear statement in a good location\r\nfid = fopen('.\\fcnTest.m', 'w');\r\nfprintf(fid, 'function fcnTest(varargin)\\ndisp(''%s Problem'')', id);\r\nfclose(fid);\r\n\r\nclear fcnTest  % or could use rehash here instead\r\n\r\nswitch lower(mode)\r\n    case 'clear+exist'\r\n        clear fcnTest\r\n        exist( 'fcnTest.m', 'file');\r\n    case 'clear'\r\n        clear fcnTest\r\n    case 'nothing'\r\nend\r\nfcnTest;\r\n\r\n##### SOURCE END ##### a4ae532911b8407dbe9a4ce94551978f\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Over the years, there have been lots of questions about when MATLAB recognizes files on the path.  Today I'm going to talk about the case where a user generates or updates MATLAB code,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/12\/11\/when-matlab-refreshes-directories\/\">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,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/165"}],"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=165"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}