C graphics
C graphics

Reputation: 7458

Matlab: sorting files according to creation date

I have a bunch of files in a directory which are not named in any order. So when I use dir function, I get them in some order. But I would like to read those files in the order of its datenum. Is there an option in dir or any other function which can achieve this task.

Upvotes: 3

Views: 10987

Answers (3)

C graphics
C graphics

Reputation: 7458

list = dir('c:\*.*');
[r inx]=sort({list.date});
list = list(inx);

Upvotes: 0

bla
bla

Reputation: 26069

here's a way to sort it out:

files=dir;
valid_file_names= find(~[files.isdir]);
file_date=[files.datenum];
[~, ix]=sort(file_date)
ix=ix(ismember(ix,valid_file_names)); keep index of file names only

     % load...
     for n=1:numel(ix)
         c{n}=your_loading_function(files(ix(n)).name);
     end         

Upvotes: 2

voxeloctree
voxeloctree

Reputation: 849

You don't need to convert the date field by datenum. Presupposing you already have the datenum, which is a field in the structure already returned by dir, i.e. if you use files = dir() then files.datenum is are the dates converted to double format. So to use them by date all you would need is:

[~,idx] = sort([files.datenum]);

Then the idx contains the values from oldest files(idx(1)) to newest files(idx(end)). Use 'descend' as an option in sort() if you want to edit the newest first.

Upvotes: 5

Related Questions