필터 지우기
필터 지우기

How to sort the order given by 'dir' command ???

조회 수: 11 (최근 30일)
Divya
Divya 2013년 6월 7일
편집: Stephen23 2021년 4월 18일
I am reading the images from a folder by using the below code
[folder1] = uigetdir();
f=dir(folder1);
for k=1:size(f,1)-2
file_n=f(k+2).name;
end
The images in the folder are "test_base1", "test_base2", .... but the order in which 'dir' taking is "test_base1","test_base10","test_base2".. I understood that the command is taking ascii-betical order.. I have gone through the previous questions and answers. It seems complicated. Can any one give a good solution to sort the order of Images after using "dir"
  댓글 수: 1
Stephen23
Stephen23 2016년 2월 22일
편집: Stephen23 2021년 4월 18일
You could download my FEX submission natsortfiles:
>> S = dir('*.txt');
>> S.name
ans =
'1.txt'
ans =
'10.txt'
ans =
'2.txt'
>> S = natsortfiles(S); % alphanumeric sort by filename
>> S.name
ans =
'1.txt'
ans =
'2.txt'
ans =
'10.txt'

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 7일
file={'test_base1','test_base10','test_base2'}
[idx,idx]=sort(cellfun(@(x) str2num(char(regexp(x,'\d*','match'))),file))
new_file=file(idx)
  댓글 수: 3
Divya
Divya 2013년 6월 8일
I am facing an error with a different input for this expression. For file = {'test_30min1','test_30min10','test_30min2'}; it gives error as follows
??? Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
I tried giving "regexp(x,'\w*','match')" and many other combinations in this expression. I am not able to get the solution. May i know what is the solution for this??
Divya
Divya 2013년 6월 8일
Hey "regexp(x,'\d*$','match')" is working for this problem. Thanks anyhow

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by