# How do I sort filenames containing text and numbers in numerical order in MATLAB?

조회 수: 163 (최근 30일)
편집: Stephen23 2021년 4월 18일
I have a series of .png files in a folder whose names are the following;
(A)Test_Nom_BAUD1.png
(A)Test_Nom_BAUD7.png
(A)Test_Nom_BAUD8.png
(A)Test_Nom_BAUD10.png
(A)Test_Nom_BAUD11.png
In reading the MATLAB documentation, I understand the dir function sorts strings in ASCII dictionary order. And since the files I'm using have no leading zeros, this is a problem. This is exactly what I'm seeing when I use the dir function:
A = dir('*.png');
(A)Test_Nom_BAUD1.png
(A)Test_Nom_BAUD10.png
(A)Test_Nom_BAUD11png
(A)Test_Nom_BAUD7.png
(A)Test_Nom_BAUD8.png
But what I need is this;
A = dir('*.png');
(A)Test_Nom_BAUD1.png
(A)Test_Nom_BAUD7.png
(A)Test_Nom_BAUD8.png
(A)Test_Nom_BAUD10.png
(A)Test_Nom_BAUD11.png
I know this can be done if the filenames contain only numbers.
But is it possible to sort these filenames (in numerical order) using existing text manipulation and sorting routines in MATLAB? If so, how?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Stephen23 2018년 9월 21일
편집: Stephen23 2021년 4월 18일
"If so, how?"
Simply download my FEX submission natsortfiles, which was written to solve that exact problem:
S = dir('*.png');
S = natsortfiles(S); % alphanumeric sort by filename

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

### 채택된 답변

Cedric 2017년 10월 10일
편집: Cedric 2017년 10월 10일
If, for any reason, you cannot install this function, you can sort your filenames as follows:
[~, reindex] = sort( str2double( regexp( {A.name}, '\d+', 'match', 'once' )))
A = A(reindex) ;
Note that it assumes that all files have the same base name. If you have to deal with various base names, then you need the function mentioned in the other answers, or to work a little more on the sorting.
##### 댓글 수: 5이전 댓글 3개 표시이전 댓글 3개 숨기기
Image Analyst 2018년 3월 23일
You'll have to write a custom parsing routine to extract out the numbers you want into numerical vectors then sort them yourself. Shouldn't be hard but let us know if you can't figure it out.
Stephen23 2018년 9월 21일
편집: Stephen23 2021년 4월 18일
"You'll have to write a custom parsing routine to extract out the numbers you want into numerical vectors then sort them yourself."
You don't have to do that at all: natsortfiles handles multiple number values already. Just provide an appropriate regular expression, and it will work just fine:
>> C = {...
'RSN1112-KOBE-1.1_Node_Floor_Dsp.out',...
'RSN1111-KOBE-0.2_Node_Floor_Dsp.out',...
'RSN1112-KOBE-0.2_Node_Floor_Dsp.out',...
'RSN1111-KOBE-0.1_Node_Floor_Dsp.out',...
'RSN1111-KOBE-1.1_Node_Floor_Dsp.out',...
'RSN1112-KOBE-0.1_Node_Floor_Dsp.out'};
>> natsortfiles(C,'\d+\.?\d*') % alphanumeric sort
ans =
'RSN1111-KOBE-0.1_Node_Floor_Dsp.out'
'RSN1111-KOBE-0.2_Node_Floor_Dsp.out'
'RSN1111-KOBE-1.1_Node_Floor_Dsp.out'
'RSN1112-KOBE-0.1_Node_Floor_Dsp.out'
'RSN1112-KOBE-0.2_Node_Floor_Dsp.out'
'RSN1112-KOBE-1.1_Node_Floor_Dsp.out'

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

### 추가 답변 (2개)

KSSV 2017년 10월 10일
##### 댓글 수: 2없음 표시없음 숨기기
Thanks, but I can't get to that WEB site from this workstation.
Jan 2017년 10월 10일
Really? But it is on the same server as the forum. Perhaps it works if you insert your country code:

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

Image Analyst 2017년 10월 10일

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

### 카테고리

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