Borrar filtros
Borrar filtros

Problem with MATLAB "exist", returning 0, although the file actually exists!

34 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 9 de Ag. de 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
John D'Errico el 9 de Ag. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 9 de Ag. de 2017
Editada: Stephen23 el 9 de Ag. de 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.
  1 comentario
Titli
Titli el 9 de Ag. de 2017
I can not thank you enough by just writing "thank you". I really needed someone to tell me to stop blaming my computer for everything and learn things properly! :D
It has helped me a lot !

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 9 de Ag. de 2017
Editada: Jan el 9 de Ag. de 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.
  1 comentario
Titli
Titli el 9 de Ag. de 2017
D='D:\2nd year\C0001.tifroiSTR1';
listings = dir(fullfile(D, '*.jpg'));
nfiles=length(listings);
for i=1:nfiles;
filename = listings(i).name;
[filepath, basename, ext] = fileparts(filename);
datfilename = fullfile( filepath, [basename '.dat']);
end
Here is the code!

Iniciar sesión para comentar.


Star Strider
Star Strider el 9 de Ag. de 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.

Categorías

Más información sobre File Name Construction en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by