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:
Sort a date vector

Subject: Sort a date vector

From: Rogelio

Date: 2 Jul, 2012 20:36:07

Message: 1 of 9

Hei !

I have a problem.... I am using textscan to download and read a textfile

[C_text B0]=textscan(fid,'%s',30,'delimiter','|','HeaderLines',4);
[C_data B1]=textscan(fid,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s','delimiter','|');

and do some computations. The file is a matrix of size [144 000 x 30] where the last 3 columns contains date and hour in this format: '2012-07-02 04:28:15.608'.

How can I sort my date as a function of one of the date-time column?

Any suggestion to where to look is appreciated!
 

Subject: Sort a date vector

From: dpb

Date: 2 Jul, 2012 20:50:31

Message: 2 of 9

On 7/2/2012 3:36 PM, Rogelio wrote:
> ...using textscan to download and read a textfile
> [C_text B0]=textscan(fid,'%s',30,'delimiter','|','HeaderLines',4);
> [C_data B1]=textscan(fid,'%s %s ... %s %s','delimiter','|');

BTW, to avoid the ungodly number of repetitions in a fmt string, do
something like

fmt=repmat('%s ',1,N); % where N is the number of fields needed
[C_data B1]=textscan(fid,fmt,'delimiter','|');

If there are various types other than just the on %s as in your case,
it's easy enough to build them by stringing together stuff inside the
square brackets "[]"

fmt=['%f repmat('%s ',1,N) '%*f']; % etc., etc., etc., ...

> and do some computations. The file is a matrix of size [144 000 x 30]
> where the last 3 columns contains date and hour in this format:
> '2012-07-02 04:28:15.608'.

That looks like two columns to me???

> How can I sort my date as a function of one of the date-time column?
>
> Any suggestion to where to look is appreciated!

doc datenum % convert, sort, datestr() to display as text again.

--

Subject: Sort a date vector

From: Rogelio

Date: 2 Jul, 2012 21:07:10

Message: 3 of 9

dpb <none@non.net> wrote in message <jst1im$u3p$1@speranza.aioe.org>...
> On 7/2/2012 3:36 PM, Rogelio wrote:
> > ...using textscan to download and read a textfile
> > [C_text B0]=textscan(fid,'%s',30,'delimiter','|','HeaderLines',4);
> > [C_data B1]=textscan(fid,'%s %s ... %s %s','delimiter','|');
>
> BTW, to avoid the ungodly number of repetitions in a fmt string, do
> something like
>
> fmt=repmat('%s ',1,N); % where N is the number of fields needed
> [C_data B1]=textscan(fid,fmt,'delimiter','|');
>
> If there are various types other than just the on %s as in your case,
> it's easy enough to build them by stringing together stuff inside the
> square brackets "[]"
>
> fmt=['%f repmat('%s ',1,N) '%*f']; % etc., etc., etc., ...
>
> > and do some computations. The file is a matrix of size [144 000 x 30]
> > where the last 3 columns contains date and hour in this format:
> > '2012-07-02 04:28:15.608'.
>
> That looks like two columns to me???
>
> > How can I sort my date as a function of one of the date-time column?
> >
> > Any suggestion to where to look is appreciated!
>
> doc datenum % convert, sort, datestr() to display as text again.
>
> --

thanks for the answer!
but '2012-07-02 04:28:15.608' is the complete format of one of the 3 columns containing date-time info. I am trying
n = datenum(2012-07-02 04:28:15.608,'yyyy-mm-dd')
but this doesnt recognize the hour.

Subject: Sort a date vector

From: dpb

Date: 2 Jul, 2012 21:56:44

Message: 4 of 9

On 7/2/2012 4:07 PM, Rogelio wrote:
...

> but '2012-07-02 04:28:15.608' is the complete format of one of the 3
> columns containing date-time info. I am trying n = datenum(2012-07-02
> 04:28:15.608,'yyyy-mm-dd') but this doesnt recognize the hour.

<http://www.mathworks.com/help/techdoc/matlab_prog/bspgcx2-1.html#bsplnu3-2>

'2012-07-02 04:28:15.608'

If you have a recent-enough version you can set up a specific formatting
string

'yyyy-mm-dd HH:MM:SS.FFF'

If not, you'll need to parse the string to a form your version can
recognize.

--

Subject: Sort a date vector

From: Rogelio

Date: 2 Jul, 2012 22:48:07

Message: 5 of 9

dpb <none@non.net> wrote in message <jst5eq$7pq$1@speranza.aioe.org>...
> On 7/2/2012 4:07 PM, Rogelio wrote:
> ...
>
> > but '2012-07-02 04:28:15.608' is the complete format of one of the 3
> > columns containing date-time info. I am trying n = datenum(2012-07-02
> > 04:28:15.608,'yyyy-mm-dd') but this doesnt recognize the hour.
>
> <http://www.mathworks.com/help/techdoc/matlab_prog/bspgcx2-1.html#bsplnu3-2>
>
> '2012-07-02 04:28:15.608'
>
> If you have a recent-enough version you can set up a specific formatting
> string
>
> 'yyyy-mm-dd HH:MM:SS.FFF'
>
> If not, you'll need to parse the string to a form your version can
> recognize.
>
> --

Thanks! I realize that by simply looping trough the column datenum will do the work!
however, the loop is kind of slow coz first i need to remove null values and then transform the date-time to number. Is it a better/faster way to do this:

for i = 1 : N
ram=C_data{1,30}{i,1};
ram1=strcmp(ram,'null');
if (ram1==1)
C_data{1,30}{i,1}=0;
else
C_data{1,30}{i,1}=C_data{1,30}{i,1};
end
end


Acopio = zeros (N,1);
for i = 1 : N
ram=C_data{1,30}{i,1};
ram1=strcmp(ram,'0');
    if (ram1~=1)
    Acopio(i,1) = datenum(C_data{1,30}{i,1});
    end
end

Subject: Sort a date vector

From: dpb

Date: 3 Jul, 2012 00:24:48

Message: 6 of 9

On 7/2/2012 5:48 PM, Rogelio wrote:
...

> Thanks! I realize that by simply looping trough the column datenum will
> do the work!
> however, the loop is kind of slow coz first i need to remove null values
> and then transform the date-time to number. Is it a better/faster way to
> do this:
>
> for i = 1 : N
> ram=C_data{1,30}{i,1};
> ram1=strcmp(ram,'null');
> if (ram1==1)
> C_data{1,30}{i,1}=0;
...snip...
       Acopio(i,1) = datenum(C_data{1,30}{i,1});
> end
> end

Sure...use the whole cell array--first find an logical vector of those
to handle, then pass to datenum()

isOK=~strcmp(C_data(1,30),'null');
acopio=datenum(C_data(1,30){isOK});

Air code above--I have neither textscan nor the multilevel indexing in
my release of Matlab, but the string functions should work ok on the
indexing into the cell, I think. I'm not sure about the second--it may
expand the isOK list into a comma-separated list instead of passing the
content cell values you wish. W/O the facility to do this I'm guessing
a little here on syntax.

If you can't figure out the right referencing in the full array, at
worst you can make a copy of the one cell and then I know how it would
work...

C=C_data(1,30); % a temporary copy
isOK=~strcmp(C,'null');
acopio=datenum(C(isOK});

Here's a little sample I did here just copying the one date over and
over and adding a 'null' here and there...

 >> s='07-02-2012 04:28:15.608'; % rearranged so datenum recognizes it
 >> c={s;s;s;s;'null';s;'null';s;'null';s;s}; % make a cell array
 >> isOK=~strcmp(c,'null'); % a logical array of good locations
 >> datenum(c(isOK)) % convert to datenum
ans =
   1.0e+005 *
     7.3505
     7.3505
     7.3505
     7.3505
     7.3505
     7.3505
     7.3505
     7.3505
 >> datestr(ans) % sanity check the result
ans =
02-Jul-2012 04:28:16
02-Jul-2012 04:28:16
02-Jul-2012 04:28:16
02-Jul-2012 04:28:16
02-Jul-2012 04:28:16
02-Jul-2012 04:28:16
02-Jul-2012 04:28:16
02-Jul-2012 04:28:16
 >>

--

Subject: Sort a date vector

From: Rogelio

Date: 3 Jul, 2012 05:30:08

Message: 7 of 9

dpb <none@non.net> wrote in message <jste4h$pc2$1@speranza.aioe.org>...
> On 7/2/2012 5:48 PM, Rogelio wrote:
> ...
>
> > Thanks! I realize that by simply looping trough the column datenum will
> > do the work!
> > however, the loop is kind of slow coz first i need to remove null values
> > and then transform the date-time to number. Is it a better/faster way to
> > do this:
> >
> > for i = 1 : N
> > ram=C_data{1,30}{i,1};
> > ram1=strcmp(ram,'null');
> > if (ram1==1)
> > C_data{1,30}{i,1}=0;
> ...snip...
> Acopio(i,1) = datenum(C_data{1,30}{i,1});
> > end
> > end
>
> Sure...use the whole cell array--first find an logical vector of those
> to handle, then pass to datenum()
>
> isOK=~strcmp(C_data(1,30),'null');
> acopio=datenum(C_data(1,30){isOK});
>
> Air code above--I have neither textscan nor the multilevel indexing in
> my release of Matlab, but the string functions should work ok on the
> indexing into the cell, I think. I'm not sure about the second--it may
> expand the isOK list into a comma-separated list instead of passing the
> content cell values you wish. W/O the facility to do this I'm guessing
> a little here on syntax.
>
> If you can't figure out the right referencing in the full array, at
> worst you can make a copy of the one cell and then I know how it would
> work...
>
> C=C_data(1,30); % a temporary copy
> isOK=~strcmp(C,'null');
> acopio=datenum(C(isOK});
>
> Here's a little sample I did here just copying the one date over and
> over and adding a 'null' here and there...
>
> >> s='07-02-2012 04:28:15.608'; % rearranged so datenum recognizes it
> >> c={s;s;s;s;'null';s;'null';s;'null';s;s}; % make a cell array
> >> isOK=~strcmp(c,'null'); % a logical array of good locations
> >> datenum(c(isOK)) % convert to datenum
> ans =
> 1.0e+005 *
> 7.3505
> 7.3505
> 7.3505
> 7.3505
> 7.3505
> 7.3505
> 7.3505
> 7.3505
> >> datestr(ans) % sanity check the result
> ans =
> 02-Jul-2012 04:28:16
> 02-Jul-2012 04:28:16
> 02-Jul-2012 04:28:16
> 02-Jul-2012 04:28:16
> 02-Jul-2012 04:28:16
> 02-Jul-2012 04:28:16
> 02-Jul-2012 04:28:16
> 02-Jul-2012 04:28:16
> >>
>
> --

Thanks dpb! your first suggestion works perfect and takes like 1/10 the time my method did!

Subject: Sort a date vector

From: dpb

Date: 3 Jul, 2012 13:07:09

Message: 8 of 9

On 7/3/2012 12:30 AM, Rogelio wrote:
> dpb <none@non.net> wrote in message <jste4h$pc2$1@speranza.aioe.org>...
...

>>
>> Sure...use the whole cell array--first find an logical vector of those
>> to handle, then pass to datenum()
>>
>> isOK=~strcmp(C_data(1,30),'null');
>> acopio=datenum(C_data(1,30){isOK});
>>
>> Air code above--I have neither textscan nor the multilevel indexing in
>> my release of Matlab, ...

...

> Thanks dpb! your first suggestion works perfect and takes like 1/10 the
> time my method did!

Well, sometimes even the blind pig... :)

I wasn't positive about the multilevel indexing curlies -- they would
have given a comma list w/ the version here w/ single-level indexing.
Glad to hear it works "as expected"...

--

Subject: Sort a date vector

From: Rogelio

Date: 10 Jul, 2012 20:21:12

Message: 9 of 9

dpb <none@non.net> wrote in message <jsuqpv$11e$1@speranza.aioe.org>...
> On 7/3/2012 12:30 AM, Rogelio wrote:
> > dpb <none@non.net> wrote in message <jste4h$pc2$1@speranza.aioe.org>...
> ...
>
> >>
> >> Sure...use the whole cell array--first find an logical vector of those
> >> to handle, then pass to datenum()
> >>
> >> isOK=~strcmp(C_data(1,30),'null');
> >> acopio=datenum(C_data(1,30){isOK});
> >>
> >> Air code above--I have neither textscan nor the multilevel indexing in
> >> my release of Matlab, ...
>
> ...
>
> > Thanks dpb! your first suggestion works perfect and takes like 1/10 the
> > time my method did!
>
> Well, sometimes even the blind pig... :)
>
> I wasn't positive about the multilevel indexing curlies -- they would
> have given a comma list w/ the version here w/ single-level indexing.
> Glad to hear it works "as expected"...
>
> --

Hi again!
I have a question ... is it possible to use this method isOK=~strcmp(C_data(1,30),'null'); with two conditions?
What I mean is something like this:
 isOK=~strcmp(C_data(1,30),'null')&&strcmp(C_data(1,20),'1'); for example. I want to add the values in one column given that two conditions are satisfied. Each logical test is in different columns.

Tags for this Thread

No tags are associated with 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