MATLAB: How to clear MATLAB’s view of a function that is neither on the path nor in the current directory

currentdirectoryMATLABrehash

I have two different, but identically-named functions, in two different directories:
/A/foo.m
/B/foo.m
Additionally, I have a directory
/C
which is not on my path, and is not the current directory.
However, when I run:
cd('/');
copyfile('/A/foo.m", "/C");
cd('/C');
foo;
cd('/');
rehash path;
copyfile('/B/foo.m", "/C");
cd('/C');
foo;
the same "foo" is run both times.

Best Answer

REHASH only causes MATLAB to check the timestamps for files on the search path. In the example, however, "/C" is not on the path when REHASH is called, and so it has no effect.
Relying on MATLAB's ability to "see" newer versions of files that are dynamically changing as it runs is risky.
That said, you can force MATLAB to reload a file by using CLEAR. For instance,
clear foo
forces MATLAB to clear "foo" from memory, meaning it must be re-read.