Corrupted Order of the Elements of struct.name After Using dir Function
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2021-12-22
编辑:Stephen23
2021-12-22
@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.
采纳的回答
Walter Roberson
2021-12-22
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 个评论
Walter Roberson
2021-12-22
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.
更多回答(1 个)
Stephen23
2021-12-22
编辑:Stephen23
2021-12-22
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!