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

5 views (last 30 days)
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.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 26 May 2011
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)

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!