Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dir sort by date

0 views
Skip to first unread message

Rajesh Rajaram

unread,
Sep 10, 2010, 1:37:11 PM9/10/10
to
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.

Thanks in advance
Rajesh

Matt Fig

unread,
Sep 10, 2010, 1:54:10 PM9/10/10
to
You can sort the names by date:

% Data
D = dir;


% Engine
S = [D(:).datenum].'; % you may want to eliminate . and .. first.
[S,S] = sort(S);
S = {D(S).name} % Cell array of names in order by datenum.

Sean

unread,
Sep 10, 2010, 1:59:26 PM9/10/10
to
"Rajesh Rajaram" <rajesh....@siemens.com> wrote in message <i6dqc7$hf7$1...@fred.mathworks.com>...

> 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.
>
> Thanks in advance
> Rajesh

Is this what you want?
A = dir;
[~,idx] = sort([A.datenum]);
then idx is the list from oldest to newest.
I.e.
oldest = A(idx(1))
newest = A(idx(end))

Rajesh Rajaram

unread,
Sep 10, 2010, 2:08:07 PM9/10/10
to
Thanks a lot. I ended up doing something very similar with the sort function. I was just hoping that there would be a more elegant option with the dir function or something.

But thanks a lot for your suggestion. I really appreciate your effort to solve my querry.

Rajesh

"Matt Fig" <spam...@yahoo.com> wrote in message <i6drc2$mgf$1...@fred.mathworks.com>...

Sean

unread,
Sep 10, 2010, 2:14:08 PM9/10/10
to
"Rajesh Rajaram" <rajesh....@siemens.com> wrote in message <i6ds67$h0v$1...@fred.mathworks.com>...

> Thanks a lot. I ended up doing something very similar with the sort function. I was just hoping that there would be a more elegant option with the dir function or something.
>
> But thanks a lot for your suggestion. I really appreciate your effort to solve my querry.
>
> Rajesh
>

You could write your own wrapper 'dir' function that masks this behind the scenes work to make it elegant:

function out = ddir;
A = dir
... %rest of engine
end

0 new messages