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

4 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 26 de Mayo de 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)

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by