Problem with MATLAB "exist", returning 0, although the file actually exists!
38 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 9 Aug. 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
am 9 Aug. 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.
Akzeptierte Antwort
Stephen23
am 9 Aug. 2017
Bearbeitet: Stephen23
am 9 Aug. 2017
This will fix it:
D = 'D:\2nd year\C0001.tifroiSTR1';
S = dir(fullfile(D,'*.jpg'));
for k = 1:numel(S);
[~,name,ext] = fileparts(S(k).name);
filename = fullfile(D,[name,'.dat']);
end
The problem with your code starts on this line:
[filepath, basename, ext] = fileparts(filename);
because filename does not contain the path information at all (only the name of the file), then filepath will be empty and totally pointless. So when you construct the filepath again using fullfile using filepath there is no path... and MATLAB looks in the current directory (which is what you are telling MATLAB to do when there is no path). I fixed this by providing the path D to fullfile, just like you did with dir yourself.
The problem is a more general one though: beginners prefer to rely on what they imagine their computer is doing, or what they wish their computer is doing. But code does not care what is in your head, nor what you want it to do. It will do what it is written to do. And your job, if you want to learn how to write good code, is to look at what the code is doing: if you had looked at your variables (which is a very basic step in bugfixing) then you would have found your path information was missing.
Weitere Antworten (2)
Jan
am 9 Aug. 2017
Bearbeitet: Jan
am 9 Aug. 2017
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
am 9 Aug. 2017
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu File Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!