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

34 visualizzazioni (ultimi 30 giorni)
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.
  2 Commenti
Dr. JANAK TRIVEDI il 31 Gen 2023
This issue could be due to the way MATLAB caches the list of functions in the path. When you add a new file to a folder that is already on the path, MATLAB may not immediately detect the change. To resolve this issue, you can use the rehash command to clear the cache and update the list of functions in the path.
Try running the following command in the MATLAB Command Window:
Zach Yadanza
Zach Yadanza il 31 Gen 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?

Accedi per commentare.

Risposta accettata

John D'Errico
John D'Errico il 5 Feb 2023
Modificato: John D'Errico il 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
  2 Commenti
Zach Yadanza
Zach Yadanza il 5 Feb 2023
That makes so much sense. I actually realize now that I use to have the files outside of the MATLAB directory, but recently removed MATLAB from my SSD since it was taking up to much space and thats when I started to have the issues.
I will go ahead and try moving the path folders outside of the MATLAB directory and see how that goes.
Zach Yadanza
Zach Yadanza il 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!

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 31 Gen 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
  5 Commenti
Stephen23 il 5 Feb 2023
"I'm not exactly sure what that means but my guess to that question would be, MATLAB/bin/Matlab Programs."
You don't know where you are saving files? That must make working on a computer very difficult.
Why why why why why are you saving files into what appears to be the installation folder of an application?
Walter Roberson
Walter Roberson il 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

Accedi per commentare.


Scopri di più su Startup and Shutdown in Help Center e File Exchange





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by