file not found in current directory

This sounds very similar to other entries in this forum and elsewhere e.g.
But a little different, and those answers don't help me. When I got this message the first time, I realised I had forgotten to copy a file across from a neighbouring folder. (I would hardlink if windows allowed that on network drives, or softlink if matlab could follow windows' softlinks... but I digress.) Even after copying the file, I still get the file not found message. Weirdly, 'edit my_function.m' works!
>> my_function
'my_function' is not found in the current folder or on
the MATLAB path, but exists in:
H:\path\to\nearby_folder
Change the MATLAB current folder or add its folder to the MATLAB
path.
>> exist('my_function')
ans =
0
>> which -all my_function.m
'my_function.m' not found.
>> edit my_function.m
>> ls
.
..
picture.png
figure.pdf
my_function.m
my_script.m

4 commentaires

Image Analyst
Image Analyst le 21 Mai 2018
What was the current directory when you were doing all of this?
Did you at any time, between these commands, change the current directory from the start to "H:\path\to\nearby_folder", or any other folder or were you in the original folder the whole time?
craq
craq le 21 Mai 2018
The file in question has been copied into the current working directory. You can see this because it comes up when I run 'ls'. I didn't change the current directory at any time.
Image Analyst
Image Analyst le 21 Mai 2018
I ask because I think the post you made has been altered. For example, I doubt that you really have a folder called "H:\path\to\nearby_folder" -- maybe you do have a folder with that exact name, but it seems like a strange folder name to have so that's why I think some things were altered and not a clean unmodified copy and paste from your command window. Again, what is the current folder?
craq
craq le 21 Mai 2018
Modifié(e) : craq le 21 Mai 2018
Yes, I altered the folder name and file names for privacy. But the function m-file is in the cwd, so the path shouldn't really matter. It is not in matlabroot.

Connectez-vous pour commenter.

 Réponse acceptée

Cam Salzberger
Cam Salzberger le 21 Mai 2018

2 votes

Hello Chris,
By default, MATLAB will cache functions and files for faster finding and executing of them. Generally files newly added to folders will be found properly, but sometimes it needs to be told to rehash before it will detect the new file. That it finds the file with edit and ls likely has more to do with those commands using a different system for finding files than the execution engine.
If rehash doesn't do it for you, try rehash toolboxcache just to be sure. Restarting MATLAB is something of the nuclear option here, but should work in almost all cases.
-Cam

4 commentaires

craq
craq le 21 Mai 2018
I went with a restart, so unfortunately I can't test whether rehash would've worked. I can confirm that restarting works ;-) and the rest sounds logical, so I've accepted your answer.
craq
craq le 3 Juil 2018
I just had this happen again. This time I tried your suggestions. Nothing changed when I ran rehash. rehash toolboxcache fixed it.
Cam Salzberger
Cam Salzberger le 19 Juil 2018
Thanks for the update. The toolboxcache should only matter if the folder you're changing the files is within the MATLAB installation or local preferences directory. If you are changing files outside of that, and you are seeing rehash by itself failing to affect it reproducibly and consistently, please contact tech support with the reproduction steps.
craq
craq le 25 Juil 2018
The first time was outside the matlab installation and outside the preferences directory. The second time I didn't record the directory I was in. It has only happened twice so far, so it's hard to talk about "reproducibility". If it happens again and I notice anything, I will contact tech support.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Produits

Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by