Corrupted Order of the Elements of struct.name After Using dir Function
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Emre Bostancioglu
el 21 de Dic. de 2021
Comentada: Emre Bostancioglu
el 22 de Dic. de 2021
I seperated a video file into its frames (in .jpg form) yet frames are more than required. Therefore, some of them needs to be deleted but as it is known, dir function does not order elements in normal numerical order. I also tried Mr. Stephen's natsortfiles. However, the resultant struct started to order the .jpg files from 100 (100.jpg-file name) as the first element. Consequently, the code deletes wrong pictures in either case. It is most probly that I am making a mistake but I cannot figure it out. Below, the script can be seen but it may not make sense since it has a previous part. That is why, I also put the Workspace returns.
If I mistakenly stated something wrong or offensive, I am terribly sorry and thanks in advance for the help.
PS: Please do not hesitate to demand more information that I will surely try to provide as much as I can.
dir_of_deletion = 'directory';
dir_content = dir(fullfile(dir_of_deletion));
dir_content([dir_content.isdir]) = [];
C=natsortfiles(dir_content); % Mr. Stephen's Work, WWill Be Cited
decisive_files = length(dir_content);
for range = 0 : decisive_files
if rem(range,90)~=0
delete(dir_content(range).name) % or delete(C(range).name)
end
range=range+1
end

I guess, because of a mistake of mine, Mr. Stephen's natsortfiles output provides an order from 100 to 1620. Then, provides an order from 10 to 99 and lastly, provides an order from 1 to 9 where 9 is the 1620th element of the struct.

This is the regular result of dir function.
3 comentarios
Stephen23
el 22 de Dic. de 2021
Editada: Stephen23
el 22 de Dic. de 2021
@Emre Bostancioglu: can you please do the following:
- upload the variable DIR_CONTENT in a mat file.
- confirm that the filenames have leading whitespace.
It appears that the order is disturbed by those leading space characters, but rather than guessing what the cause is, a robust answer will need to be diagnosed and tested using your actual data.
Respuesta aceptada
Walter Roberson
el 22 de Dic. de 2021
dir_of_deletion = 'directory';
dir_content = dir(fullfile(dir_of_deletion));
dir_content([dir_content.isdir]) = [];
[~, basenames, ~] = fileparts({dir_content.name});
basenum = str2double(basenames);
[~, order] = sort(basenum);
sorted_dir = dir_content(order);
3 comentarios
Walter Roberson
el 22 de Dic. de 2021
dir_of_deletion = 'directory';
dir_content = dir(fullfile(dir_of_deletion));
dir_content([dir_content.isdir]) = [];
[~, basenames, ~] = fileparts({dir_content.name});
basenum = str2double(strtrim(basenames));
[~, order] = sort(basenum);
sorted_dir = dir_content(order);
Fixed to ignore leading and trailing whitespace in the numbers.
Más respuestas (1)
Stephen23
el 22 de Dic. de 2021
Editada: Stephen23
el 22 de Dic. de 2021
For those filenames you can simply define the regular expression to include leading whitespace:
C = {'100.jpg',' 99jpg'}; % check examples in the question!
D = natsortfiles(C,'\s*\d+')
I might change the NATSORT default to ignore leading space characters, and add some examples.
Thank you for the idea!
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!