Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
dir sort by date

Subject: dir sort by date

From: Rajesh Rajaram

Date: 10 Sep, 2010 17:37:11

Message: 1 of 5

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

Subject: dir sort by date

From: Matt Fig

Date: 10 Sep, 2010 17:54:10

Message: 2 of 5

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.

Subject: dir sort by date

From: Sean

Date: 10 Sep, 2010 17:59:26

Message: 3 of 5

"Rajesh Rajaram" <rajesh.rajaram@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))

Subject: dir sort by date

From: Rajesh Rajaram

Date: 10 Sep, 2010 18:08:07

Message: 4 of 5

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" <spamanon@yahoo.com> wrote in message <i6drc2$mgf$1@fred.mathworks.com>...
> 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.

Subject: dir sort by date

From: Sean

Date: 10 Sep, 2010 18:14:08

Message: 5 of 5

"Rajesh Rajaram" <rajesh.rajaram@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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us