Files added to path won't run unless I rename them first.
3 views (last 30 days)
Show older comments
Commented: Walter Roberson on 5 Feb 2023
I've been working with matlab for a little over a year now but towards the end of 2022 I found a issue where I would create a new script and add it to a folder that was already added to the path, but when I try to run it it says it's not in the path. However, once I rename the files then it seems to have no issue.
This is currently not a big issue and I hope it never will be but I am confused and it is a quality of life issue that I would like to fix.
John D'Errico on 5 Feb 2023
Edited: John D'Errico on 5 Feb 2023
Never use the directory structure that comes with MATLAB to store your own files. Why not?
MATLAB caches those directories at startup. That means it will not see any changes made to your files. Of course, when you change the file, MATLAB will not know that, because it is still using the cached version.
Can you force MATLAB to see the change? Well, yes. You can issue a rehash command, forcing MATLAB to redo the cache on all of its files. Is that a good idea? Not really. It takes time. It forces you to remember to rehash the cache every time you make a minor change.
Instead, use your own directories to store your own m-files. This is a good idea anyway, since if you download a new release of MATLAB, then your files will no longer be found. All you need to do then is make sure you add your directory to the search path, and then save the path.
More Answers (1)
Walter Roberson on 31 Jan 2023
How are you creating the new script?
If you are creating it outside of MATLAB then check Preferences -> Editor/Debugger -> Automatic file changes -> Reload unedited files that have been externally modified
If there is an existing handle to a function in the old file, then you need to "clear" and the name of the file, such as
if you modified MyFunction.m
Walter Roberson on 5 Feb 2023
MATLAB always assumes that anyone who adds new code inside of one of the installation directories will use rehash to inform it about the change.
This is in contrast to the other directories on the path, including the user's current directory: it continually scans those to see if new files have appeared and if so whether an old function needs to be removed from memory in order to use the new function.
Please have a look at userpath
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!