Corrupted Order of the Elements of struct.name After Using dir Function

2 Ansichten (letzte 30 Tage)
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 Kommentare
Stephen23
Stephen23 am 22 Dez. 2021
Bearbeitet: Stephen23 am 22 Dez. 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.
Emre Bostancioglu
Emre Bostancioglu am 22 Dez. 2021
I tried the first advice and as just you said above "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 tested on your actual data.", I guess, because of this, the same output returned and for the recent recommandations of yours, I will try them as soon as I get back on my computer.
Thanks for the help!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Dez. 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 Kommentare
Stephen23
Stephen23 am 22 Dez. 2021
Bearbeitet: Stephen23 am 22 Dez. 2021
This will fail if the names have leading whitespace, as the ones in the question show:
C = {'100.jpg',' 99jpg'}; % check examples in the question!
[~,B,~] = fileparts(C);
N = str2double(B)
N = 1×2
100 NaN
Walter Roberson
Walter Roberson am 22 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 22 Dez. 2021
Bearbeitet: Stephen23 am 22 Dez. 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+')
D = 1×2 cell array
{' 99jpg'} {'100.jpg'}
I might change the NATSORT default to ignore leading space characters, and add some examples.
Thank you for the idea!
  1 Kommentar
Emre Bostancioglu
Emre Bostancioglu am 22 Dez. 2021
It is my pleasure sir! Thank you for your help and sorry that I responded late. It works fine now and thank you a lot for your effort and contribution. Is there any way that I can accept both of the answers/comments since they are working good?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by