Files added to path won't run unless I rename them first.

3 views (last 30 days)
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.
Zach Yadanza
Zach Yadanza on 31 Jan 2023
Looks like that did the trick. Is there anything I can do so that I don't have to type in rehash in the command window everytime I create a new file?

Sign in to comment.

Accepted Answer

John D'Errico
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.
help pathtool
PATHTOOL Open Set Path dialog box to view and change search path PATHTOOL opens the MATLAB Set Path tool, which allows you to view, manipulate, and save the MATLAB search path. See also PATH, ADDPATH, RMPATH, SAVEPATH. Documentation for pathtool doc pathtool Folders named pathtool matlab/pathtool
Zach Yadanza
Zach Yadanza on 5 Feb 2023
It looks like that solved my problem! I usually just add things to the path either from the tool bar at the top or from the current folder tab on the left. I appreciate your help though!

Sign in to comment.

More Answers (1)

Walter Roberson
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
clear MyFunction
if you modified MyFunction.m
Walter Roberson
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

Sign in to comment.


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!

Translated by