Sorting files inside a folder

I have files named
1 1a.bmp
1 1b.bmp
1 2a.bmp
1 2b.bmp
...
2 1a.bmp
2 1b.bmp
2 2a.bmp
2 2b.bmp
....
23 1a.bmp
23 1b. bmp
and so on
in a single folder. I have to sort these files in the order
1 1a.bmp
1 1b.bmp
2 1a.bmp
2 1b.bmp
3 1a.bmp
3 1b.bmp
...
23 1a.bmp
23 1b.bmp
1 2a.bmp
1 2b.bmp
....
23 2a.bmp
23 2b.bmp
and so on. Kindly help me with the syntax for this in Matlab.

1 comentario

Stephen23
Stephen23 el 17 de En. de 2017
Editada: Stephen23 el 17 de En. de 2017
The best solution would be to name the files so that the names could be sorted by any standard sort algorithm. That would mean placing the most significant field first, and the least significant last, and using leading zeros.
Why is this a better solution? Because fixing the problem is always better than writing hack code to try and "fix" it later.
You might like to experiment with my FEX submission:

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 17 de En. de 2017
Editada: Stephen23 el 19 de En. de 2017
This code gives the order requested in the original question:
C = {...
'1 1a.bmp';...
'1 1b.bmp';...
'1 2a.bmp';...
'1 2b.bmp';...
'2 1a.bmp';...
'2 1b.bmp';...
'2 2a.bmp';...
'2 2b.bmp';...
'23 1a.bmp';...
'23 1b.bmp';...
};
%
fun = @(s)sscanf(s,'%u%u%c.bmp').';
D = cellfun(fun,C,'UniformOutput',false);
[~,idx] = sortrows(cell2mat(D),[2,1,3]);
out = C(idx)
generating this:
out =
'1 1a.bmp'
'1 1b.bmp'
'2 1a.bmp'
'2 1b.bmp'
'23 1a.bmp'
'23 1b.bmp'
'1 2a.bmp'
'1 2b.bmp'
'2 2a.bmp'
'2 2b.bmp'

13 comentarios

shivasakthi
shivasakthi el 18 de En. de 2017
Tats great Stephen. By the way, my image folder (say chrom1) contains around 5000 images in the manner specified above. Kindly help me with the modified code to call that folder (say from D:\chrom1) and perform the operation you have specified above so that the files now in chrom1 are modified as per the above order. Thanks for your patience.
Stephen23
Stephen23 el 18 de En. de 2017
Editada: Stephen23 el 18 de En. de 2017
You can read the filenames into MATLAB like so:
S = dir('D:\chrom1*.bmp');
C = {S.name}';
and then using my code above.
"so that the files now in chrom1 are modified as per the above order"
MATLAB has no control over how files are sorted by Windows in a folder. If you want Windows to sort the files differently then you need to change the filenames or change Windows. MATLAB cannot change the Windows folder sort order.
shivasakthi
shivasakthi el 18 de En. de 2017
Editada: Stephen23 el 18 de En. de 2017
Hi Stephen I tried the way you said and I got the following error:
>> S = dir('Q:\Chrom1*.bmp');
>> C={S.name};
>> fun = @(s)sscanf(s,'%u%u%c.bmp').';
>> D = cellfun(fun,C,'UniformOutput',false);
>> [~,idx] = sortrows(cell2mat(D),[2,1,3]);
It says: Error using sortrows (line 62)
FYI, Chrom1 contains 5036 files of the form 1 1a.bmp, 1 1b.bmp, ....,23 1a.bmp, 23 1b.bmp,...,23 4a.bmp, 23 4b.bmp and so on. Kindly help. COL must be a vector of column indices into X.
shivasakthi
shivasakthi el 18 de En. de 2017
The error statement was:
Error using sortrows (line 62) COL must be a vector of column indices into X.
Thanks for your assistance
Stephen23
Stephen23 el 18 de En. de 2017
@shivasakthi: You need to transpose the cell array. Note the ':
C = {S.name}';
shivasakthi
shivasakthi el 18 de En. de 2017
Hi Stephen,
That was a typo. I have transposed the array before and got that error message.
>>S = dir('Q:\Chrom1*.bmp');
>> C={S.name}';
>> fun = @(s)sscanf(s,'%u%u%c.bmp').';
>> D = cellfun(fun,C,'UniformOutput',false);
>> [~,idx] = sortrows(cell2mat(D),[2,1,3]);
Error using sortrows (line 62) COL must be a vector of column indices into X.
Wish to have your assistance
Stephen23
Stephen23 el 19 de En. de 2017
Editada: Stephen23 el 19 de En. de 2017
Sorry, that was my mistake, the dir file match string needs the last file separator, otherwise MATLAB looks for files with the name ChromX.bmp for some X. You need to use this:
S = dir('D:\chrom1\*.bmp');
or even better is to use fullfile:
S = dir(fullfile('D:\chrom1','*.bmp'))
You can check this yourself: the returned structure S should have exactly as many elements as the number of bmp files in that folder:
numel(S)
Here is the complete, corrected code:
sub = 'D:\chrom1';
S = dir(fullfile(sub,'*.bmp'));
C = {S.name}';
fun = @(s)sscanf(s,'%u%u%c.bmp').';
D = cellfun(fun,C(:),'UniformOutput',false);
[~,idx] = sortrows(cell2mat(D),[2,1,3]);
out = C(idx)
Note that this code will only work correctly for filenames that exactly match the format that you have given in your question.
shivasakthi
shivasakthi el 19 de En. de 2017
Hi Stephen,
That perfectly worked. Thanks a lot. By the way, the variable C is getting displayed as a 5034 x 1 cell in the Matlab workspace perfectly in the preferred order. How do I save it as a separate folder (say in the D:\) with all the images arranged in the programmed order so that I can extract the images of my preference to perform an operation? Kindly assist.
shivasakthi
shivasakthi el 19 de En. de 2017
Hi Stephen,
Again, that was a typo. The variable "out" is getting displayed as a 5034 x 1 cell in the Matlab workspace perfectly in the preferred order. How do I save it as a separate folder (say in the D:\) with all the images arranged in the programmed order so that I can extract the images of my preference to perform an operation? Kindly assist.
Stephen23
Stephen23 el 19 de En. de 2017
Editada: Stephen23 el 19 de En. de 2017
"How do I save it as a separate folder (say in the D:\) with all the images arranged in the programmed order"
It is not possible to change the Windows sort order. You will have to change the filenames if you want to change the order that Windows sorts them in to.
shivasakthi
shivasakthi el 19 de En. de 2017
Hi Stephen, Thanks for the reply. Then, I need to get these done in the workspace itself.
How do I display the image of the first entry (or say any entry) in the cell array (5034 x 1)?
How can I extract the first few images (say the first 100 from the 5034 x 1) and assign it to a different variable?
Thanks for your assistance.
Stephen23
Stephen23 el 19 de En. de 2017
Editada: Stephen23 el 19 de En. de 2017
Run all of my code, and then this:
N = 100;
Z = cell(1,N);
for k = 1:N
str = fullfile(sub,out{k});
Z{k} = imread(str);
end
"assign it to a different variable?" bad idea. Read this:
shivasakthi
shivasakthi el 25 de En. de 2017
Hi Stephen,
That was great. I have also sorted my files in the windows folder using special sorting techniques suggested by the windows community. By the way, I am proposing to apply translation, at varying levels, to a set of 100 images in a folder named "chrom" in D:\. After applying the translations, I want these images to be stored in a different folder say "chrom-translate1" in the same D:\. In this way, I need to apply translations of different degrees, and store it in different folders in D:\. Kindly help me with the code for this. Thanks in advance.

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 17 de En. de 2017

1 voto

1 comentario

Stephen23
Stephen23 el 17 de En. de 2017
@Image Analyst: it is not quite a natural order sort, because the fields (characters or numbers) are not parsed from from left to right.

Iniciar sesión para comentar.

Andrei Bobrov
Andrei Bobrov el 18 de En. de 2017
My small contribution
x = regexp(C,'(\d+) (\d+)([ab])','tokens','once');
y = cat(2,x{:});
[~,ii] = sortrows([str2double(y(1:2,:));cellfun(@(x)x-'0',y(end,:))]',[2 1 3]);
out = C(ii)

1 comentario

shivasakthi
shivasakthi el 18 de En. de 2017
Editada: Image Analyst el 18 de En. de 2017
Hi Andrei,
FYI, Chrom1 is a folder that contains 5036 files of the form 1 1a.bmp, 1 1b.bmp, ....,23 1a.bmp, 23 1b.bmp,...,23 4a.bmp, 23 4b.bmp and so on, as posed in my question before.
I ran the following code along with your suggested code and got the error:
>> S = dir('Q:\chrom1*.bmp');
>> C = {S.name}';
>> x = regexp(C,'(\d+) (\d+)([ab])','tokens','once');
>> [~,ii] = sortrows([str2double(y(1:2,:));cellfun(@(x)x-'0',y(end,:))]',[2 1 3]);
Error: Index exceeds matrix dimensions.
Can you help getting it sorted? Thanks for your assistance.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 17 de En. de 2017

Comentada:

el 25 de En. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by