Sorting files inside a folder

조회 수: 11 (최근 30일)
shivasakthi
shivasakthi 2017년 1월 17일
댓글: shivasakthi 2017년 1월 25일
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
Stephen23
Stephen23 2017년 1월 17일
편집: Stephen23 2017년 1월 17일
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:

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2017년 1월 17일
편집: Stephen23 2017년 1월 19일
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
Stephen23
Stephen23 2017년 1월 19일
편집: Stephen23 2017년 1월 19일
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 2017년 1월 25일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2017년 1월 17일
  댓글 수: 1
Stephen23
Stephen23 2017년 1월 17일
@Image Analyst: it is not quite a natural order sort, because the fields (characters or numbers) are not parsed from from left to right.

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2017년 1월 18일
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
shivasakthi
shivasakthi 2017년 1월 18일
편집: Image Analyst 2017년 1월 18일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by