Import files from a folder into Matlab based on the files name

조회 수: 4 (최근 30일)
Eric
Eric 2011년 5월 26일
Greetings. I face problem in extracting 400+ files from a folder into Matlab. Those files are labeled as: splash1, splash2, splash3....splash488 The code I use is:
a ='C:\Users\Ace\Desktop\reTest\';
A =dir( fullfile(a, '*.bmp') );
fileNames = { A.name };
The problem comes when I realise the 'fileNames' array contains elements which are labeled as: splash1, splash10, splash100, splash101, splash102... like in a binary sequence.
I need the Matlab to extract the files from the folder as: splash1, splash2, splash3, ....splash488
Maybe I have missed out a clue or a more specific or accurate function. Any help, comment is welcomed! Thanks.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 5월 26일
So you want to sort your file names according to the value of the numeric part of the file name. This should work.
filenames={'sp1','sp10','sp2','sp20','sp3','sp30'};
a=regexp(filenames,'\d+','match');
b=[a{:}];
c=cellfun(@str2num,b);
[d,index]=sort(c);
NewFilenames=filenames(index)

추가 답변 (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