Problem with MATLAB "exist", returning 0, although the file actually exists!
Afficher commentaires plus anciens
Hi, I am new to MATLAB and writing a code that will find out if any file of a particular name(say, C1_C0001_STR_1.dat) exists and then it will save the .dat file into a different folder.
I am trying to use exist but it is giving 0 as return , though I have manually searched and found it exists. I dont understand if it is a glitch of my computer or if there is any problem in the code.
Any help is highly appreciated. :)
2 commentaires
Stephen23
le 9 Août 2017
Answer: there is a problem in your code.
Most likely you did not give the correct path, or the name is spelled incorrectly.
John D'Errico
le 9 Août 2017
I'm guessing the file does exist, but it is not on your search path. Therefore MATLAB does not find it. Exist does not look everywhere on your drive, but only where you tell MATLAB to search. That is what the search path does.
Réponse acceptée
Plus de réponses (2)
If exist() cannot find the file, it does not exist in the current folder and the folder of Matlab's path. It can be very tricky to use exist to check the existence of files, so please post the code you run. See https://www.mathworks.com/matlabcentral/answers/52705-test-existence-of-files-with-exist
Perhaps you specified the file without an absolute path name and the current folder is not what you expect it to be. Then do not use cd(), bit provide the complete path of the file instead.
Star Strider
le 9 Août 2017
1 vote
I always use the which (link) function to find files, since it returns the path as well. If the result is not empty (so isempty returns logical 0), the file exists. And you also have its path.
Catégories
En savoir plus sur Genomics and Next Generation Sequencing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!