Thread Subject: converting dates to intervals

Subject: converting dates to intervals

From: Janis Weir

Date: 25 Nov, 2009 15:37:03

Message: 1 of 10

I'm new to MATLAB and need to convert a time series into five-minute intervals.

That is I have data that resembles the following
13/04/1987 08:01:04
13/04/1987 08:09:31
13/04/1987 08:11:09
[etc]

and would like it to be converted to
13/04/1987 08:00:00
13/04/1987 08:05:00
13/04/1987 08:10:00
[etc]

Any suggestions?

Subject: converting dates to intervals

From: dpb

Date: 25 Nov, 2009 15:46:21

Message: 2 of 10

Janis Weir wrote:
> I'm new to MATLAB and need to convert a time series into five-minute intervals.
>
> That is I have data that resembles the following
> 13/04/1987 08:01:04
> 13/04/1987 08:09:31
> 13/04/1987 08:11:09
> [etc]
>
> and would like it to be converted to
> 13/04/1987 08:00:00
> 13/04/1987 08:05:00
> 13/04/1987 08:10:00
> [etc]
>
> Any suggestions?

A definition of "converted" would be nice...you mean consolidate to
nearest 5-min interval, simply change the time value to nearest
5-minutes, interpolate the data at the given time to 5-minute sampled
data, ... ???

--

Subject: converting dates to intervals

From: Janis Weir

Date: 25 Nov, 2009 16:03:20

Message: 3 of 10

dpb <none@non.net> wrote in message <hejjgn$40b$2@news.eternal-september.org>...
> Janis Weir wrote:
> > I'm new to MATLAB and need to convert a time series into five-minute intervals.
> >
> > That is I have data that resembles the following
> > 13/04/1987 08:01:04
> > 13/04/1987 08:09:31
> > 13/04/1987 08:11:09
> > [etc]
> >
> > and would like it to be converted to
> > 13/04/1987 08:00:00
> > 13/04/1987 08:05:00
> > 13/04/1987 08:10:00
> > [etc]
> >
> > Any suggestions?
>
> A definition of "converted" would be nice...you mean consolidate to
> nearest 5-min interval, simply change the time value to nearest
> 5-minutes, interpolate the data at the given time to 5-minute sampled
> data, ... ???
>
> Starting at 08:00:00 I would like to consolidate date into 5-minute intervals. I don't want to consolidate to the nearest 5-min interval, but rather to the interval within which the datetime falls. Take 13/04/1987 08:09:31, for example. I don't want to round to the nearest 5-minute interval, ie: 08:10:00, but rather to 08:05:00. I would like to avoid having to adjust the data manually as my series is quite large. Thanks.

Subject: converting dates to intervals

From: dpb

Date: 25 Nov, 2009 18:06:54

Message: 4 of 10

Janis Weir wrote:
> dpb <none@non.net> wrote in message <hejjgn$40b$2@news.eternal-september.org>...
>> Janis Weir wrote:
>>> I'm new to MATLAB and need to convert a time series into five-minute intervals.
>>>
>>> That is I have data that resembles the following
>>> 13/04/1987 08:01:04
>>> 13/04/1987 08:09:31
>>> 13/04/1987 08:11:09
>>> [etc]
>>>
>>> and would like it to be converted to
>>> 13/04/1987 08:00:00
>>> 13/04/1987 08:05:00
>>> 13/04/1987 08:10:00
>>> [etc]
>>>
>>> Any suggestions?
>> A definition of "converted" would be nice...you mean consolidate to
>> nearest 5-min interval, simply change the time value to nearest
>> 5-minutes, interpolate the data at the given time to 5-minute sampled
>> data, ... ???
>>
>> Starting at 08:00:00 I would like to consolidate date into 5-minute
>> intervals. I don't want to consolidate to the nearest 5-min
>> interval, but rather to the interval within which the datetime
>> falls. Take 13/04/1987 08:09:31, for example. I don't want to
>> round to the nearest 5-minute interval, ie: 08:10:00, but rather to
>> 08:05:00. I would like to avoid having to adjust the data manually
>> as my series is quite large. Thanks.

 >> ts={'08:01:04' '08:09:31' '08:11:09'};
 >> [x,x,x,h,m,s]=datevec(ts);
 >> m = m-mod(m,5)
m =
      0
      5
     10
 >> s = 0;

Put back into string w/ datestr()

--

Subject: converting dates to intervals

From: Janis Weir

Date: 25 Nov, 2009 19:25:20

Message: 5 of 10

dpb <none@non.net> wrote in message <hejrod$sjg$1@news.eternal-september.org>...
> Janis Weir wrote:
> > dpb <none@non.net> wrote in message <hejjgn$40b$2@news.eternal-september.org>...
> >> Janis Weir wrote:
> >>> I'm new to MATLAB and need to convert a time series into five-minute intervals.
> >>>
> >>> That is I have data that resembles the following
> >>> 13/04/1987 08:01:04
> >>> 13/04/1987 08:09:31
> >>> 13/04/1987 08:11:09
> >>> [etc]
> >>>
> >>> and would like it to be converted to
> >>> 13/04/1987 08:00:00
> >>> 13/04/1987 08:05:00
> >>> 13/04/1987 08:10:00
> >>> [etc]
> >>>
> >>> Any suggestions?
> >> A definition of "converted" would be nice...you mean consolidate to
> >> nearest 5-min interval, simply change the time value to nearest
> >> 5-minutes, interpolate the data at the given time to 5-minute sampled
> >> data, ... ???
> >>
> >> Starting at 08:00:00 I would like to consolidate date into 5-minute
> >> intervals. I don't want to consolidate to the nearest 5-min
> >> interval, but rather to the interval within which the datetime
> >> falls. Take 13/04/1987 08:09:31, for example. I don't want to
> >> round to the nearest 5-minute interval, ie: 08:10:00, but rather to
> >> 08:05:00. I would like to avoid having to adjust the data manually
> >> as my series is quite large. Thanks.
>
> >> ts={'08:01:04' '08:09:31' '08:11:09'};
> >> [x,x,x,h,m,s]=datevec(ts);
> >> m = m-mod(m,5)
> m =
> 0
> 5
> 10
> >> s = 0;
>
> Put back into string w/ datestr()
>
> --Perfect, thanks. Quick question, once I have broken down the datevec into individual h, m, and s vectors, how do I reverse this and consolidate h, m, and s into one vector that I can then use the datestr() command on?

Subject: converting dates to intervals

From: dpb

Date: 25 Nov, 2009 19:39:28

Message: 6 of 10

Janis Weir wrote:
> dpb <none@non.net> wrote in message <hejrod$sjg$1@news.eternal-september.org>...
>> Janis Weir wrote:
...
>>>> Starting at 08:00:00 I would like to consolidate date into 5-minute
>>>> intervals. I don't want to consolidate to the nearest 5-min
>>>> interval, but rather to the interval within which the datetime
>>>> falls. Take 13/04/1987 08:09:31, for example. I don't want to
>>>> round to the nearest 5-minute interval, ie: 08:10:00, but rather to
>>>> 08:05:00. ...

>> >> ts={'08:01:04' '08:09:31' '08:11:09'};
>> >> [x,x,x,h,m,s]=datevec(ts);
>> >> m = m-mod(m,5)
>> m =
>> 0
>> 5
>> 10
>> >> s = 0;
>>
>> Put back into string w/ datestr()
>>
>> --Perfect, thanks. Quick question, once I have broken down the
>> datevec into individual h, m, and s vectors, how do I reverse this
>> and consolidate h, m, and s into one vector that I can then use the
>> datestr() command on?

 >> datestr(datenum(x,x,x,h,m,s),13)
ans =
08:00:00
08:05:00
08:10:00
 >>

In the whole thing you may have to keep mn,da,yr as well depending on
whether the string is one or two or how you choose to treat it...I
simply used the time string for example...x==0 here, of course.

--

Subject: converting dates to intervals

From: Janis Weir

Date: 25 Nov, 2009 20:16:05

Message: 7 of 10

dpb <none@non.net> wrote in message <hek15v$fui$1@news.eternal-september.org>...
> Janis Weir wrote:
> > dpb <none@non.net> wrote in message <hejrod$sjg$1@news.eternal-september.org>...
> >> Janis Weir wrote:
> ...
> >>>> Starting at 08:00:00 I would like to consolidate date into 5-minute
> >>>> intervals. I don't want to consolidate to the nearest 5-min
> >>>> interval, but rather to the interval within which the datetime
> >>>> falls. Take 13/04/1987 08:09:31, for example. I don't want to
> >>>> round to the nearest 5-minute interval, ie: 08:10:00, but rather to
> >>>> 08:05:00. ...
>
> >> >> ts={'08:01:04' '08:09:31' '08:11:09'};
> >> >> [x,x,x,h,m,s]=datevec(ts);
> >> >> m = m-mod(m,5)
> >> m =
> >> 0
> >> 5
> >> 10
> >> >> s = 0;
> >>
> >> Put back into string w/ datestr()
> >>
> >> --Perfect, thanks. Quick question, once I have broken down the
> >> datevec into individual h, m, and s vectors, how do I reverse this
> >> and consolidate h, m, and s into one vector that I can then use the
> >> datestr() command on?
>
> >> datestr(datenum(x,x,x,h,m,s),13)
> ans =
> 08:00:00
> 08:05:00
> 08:10:00
> >>
>
> In the whole thing you may have to keep mn,da,yr as well depending on
> whether the string is one or two or how you choose to treat it...I
> simply used the time string for example...x==0 here, of course.
>
> --The dates in my excel file are formatted like this: 11/01/2002 14:37
However, once I import this data into Matlab the same value is formatted lie this: '11/01/2002 2:37:35 PM' in which case the above code does not work. Any suggestions?

Subject: converting dates to intervals

From: dpb

Date: 25 Nov, 2009 20:36:37

Message: 8 of 10

Janis Weir wrote:
> dpb <none@non.net> wrote in message <hek15v$fui$1@news.eternal-september.org>...
>> Janis Weir wrote:
>>> dpb <none@non.net> wrote in message <hejrod$sjg$1@news.eternal-september.org>...
>>>> Janis Weir wrote:
>> ...
>>>>>> Starting at 08:00:00 I would like to consolidate date into 5-minute
>>>>>> intervals. I don't want to consolidate to the nearest 5-min
>>>>>> interval, but rather to the interval within which the datetime
>>>>>> falls. Take 13/04/1987 08:09:31, for example. I don't want to
>>>>>> round to the nearest 5-minute interval, ie: 08:10:00, but rather to
>>>>>> 08:05:00. ...
>>>> >> ts={'08:01:04' '08:09:31' '08:11:09'};
>>>> >> [x,x,x,h,m,s]=datevec(ts);
>>>> >> m = m-mod(m,5)
>>>> m =
>>>> 0
>>>> 5
>>>> 10
>>>> >> s = 0;
>>>>
>>>> Put back into string w/ datestr()
>>>>
>>>> --Perfect, thanks. Quick question, once I have broken down the
>>>> datevec into individual h, m, and s vectors, how do I reverse this
>>>> and consolidate h, m, and s into one vector that I can then use the
>>>> datestr() command on?
>> >> datestr(datenum(x,x,x,h,m,s),13)
>> ans =
>> 08:00:00
>> 08:05:00
>> 08:10:00
>> >>
>>
>> In the whole thing you may have to keep mn,da,yr as well depending on
>> whether the string is one or two or how you choose to treat it...I
>> simply used the time string for example...x==0 here, of course.
>>
>> --The dates in my excel file are formatted like this: 11/01/2002 14:37
> However, once I import this data into Matlab the same value is
> formatted lie this: '11/01/2002 2:37:35 PM' in which case the above
> code does not work. Any suggestions?

Use datenum() on the time string then pass that to datevec()

Alternatively, if you have a current version of ML I believe datevec()
now has the option to specify a date/time format string. Which version
introduced that I don't know; it postdates mine so consult your
documentation.

Alternatively, I don't know if there's a way to force the 24-hr format
when importing or not w/ some of the later versions rather than the
default of am/pm (which seems a silly thing to do imo, but TMW didn't
ask :) (me at least) ).

--

Subject: converting dates to intervals

From: Janis Weir

Date: 25 Nov, 2009 20:52:19

Message: 9 of 10

dpb <none@non.net> wrote in message <hek4h4$fnt$1@news.eternal-september.org>...
> Janis Weir wrote:
> > dpb <none@non.net> wrote in message <hek15v$fui$1@news.eternal-september.org>...
> >> Janis Weir wrote:
> >>> dpb <none@non.net> wrote in message <hejrod$sjg$1@news.eternal-september.org>...
> >>>> Janis Weir wrote:
> >> ...
> >>>>>> Starting at 08:00:00 I would like to consolidate date into 5-minute
> >>>>>> intervals. I don't want to consolidate to the nearest 5-min
> >>>>>> interval, but rather to the interval within which the datetime
> >>>>>> falls. Take 13/04/1987 08:09:31, for example. I don't want to
> >>>>>> round to the nearest 5-minute interval, ie: 08:10:00, but rather to
> >>>>>> 08:05:00. ...
> >>>> >> ts={'08:01:04' '08:09:31' '08:11:09'};
> >>>> >> [x,x,x,h,m,s]=datevec(ts);
> >>>> >> m = m-mod(m,5)
> >>>> m =
> >>>> 0
> >>>> 5
> >>>> 10
> >>>> >> s = 0;
> >>>>
> >>>> Put back into string w/ datestr()
> >>>>
> >>>> --Perfect, thanks. Quick question, once I have broken down the
> >>>> datevec into individual h, m, and s vectors, how do I reverse this
> >>>> and consolidate h, m, and s into one vector that I can then use the
> >>>> datestr() command on?
> >> >> datestr(datenum(x,x,x,h,m,s),13)
> >> ans =
> >> 08:00:00
> >> 08:05:00
> >> 08:10:00
> >> >>
> >>
> >> In the whole thing you may have to keep mn,da,yr as well depending on
> >> whether the string is one or two or how you choose to treat it...I
> >> simply used the time string for example...x==0 here, of course.
> >>
> >> --The dates in my excel file are formatted like this: 11/01/2002 14:37
> > However, once I import this data into Matlab the same value is
> > formatted lie this: '11/01/2002 2:37:35 PM' in which case the above
> > code does not work. Any suggestions?
>
> Use datenum() on the time string then pass that to datevec()
>
> Alternatively, if you have a current version of ML I believe datevec()
> now has the option to specify a date/time format string. Which version
> introduced that I don't know; it postdates mine so consult your
> documentation.
>
> Alternatively, I don't know if there's a way to force the 24-hr format
> when importing or not w/ some of the later versions rather than the
> default of am/pm (which seems a silly thing to do imo, but TMW didn't
> ask :) (me at least) ).
>
> --I have tried to specify a date/time format string with datevec, but it does not seem to work. Can you give me an example of what you mean by using datenum() on the time string then passing it to datevec(). I'm not sure that I follow. Both the date and time are combined within a single cell of my series.

Subject: converting dates to intervals

From: dpb

Date: 25 Nov, 2009 21:14:06

Message: 10 of 10

Janis Weir wrote:
> dpb <none@non.net> wrote in message <hek4h4$fnt$1@news.eternal-september.org>...
>> Janis Weir wrote:
>>> dpb <none@non.net> wrote in message <hek15v$fui$1@news.eternal-september.org>...
>>>> Janis Weir wrote:
>>>>> dpb <none@non.net> wrote in message <hejrod$sjg$1@news.eternal-september.org>...
>>>>>> Janis Weir wrote:
>>>> ...
>>>>>>>> Starting at 08:00:00 I would like to consolidate date into 5-minute
>>>>>>>> intervals. I don't want to consolidate to the nearest 5-min
>>>>>>>> interval, but rather to the interval within which the datetime
>>>>>>>> falls. Take 13/04/1987 08:09:31, for example. I don't want to
>>>>>>>> round to the nearest 5-minute interval, ie: 08:10:00, but rather to
>>>>>>>> 08:05:00. ...
>>>>>> >> ts={'08:01:04' '08:09:31' '08:11:09'};
>>>>>> >> [x,x,x,h,m,s]=datevec(ts);
>>>>>> >> m = m-mod(m,5)
>>>>>> m =
>>>>>> 0
>>>>>> 5
>>>>>> 10
>>>>>> >> s = 0;
>>>>>>
>>>>>> Put back into string w/ datestr()
>>>>>>
>>>>>> --Perfect, thanks. Quick question, once I have broken down the
>>>>>> datevec into individual h, m, and s vectors, how do I reverse this
>>>>>> and consolidate h, m, and s into one vector that I can then use the
>>>>>> datestr() command on?
>>>> >> datestr(datenum(x,x,x,h,m,s),13)
>>>> ans =
>>>> 08:00:00
>>>> 08:05:00
>>>> 08:10:00
>>>> >>
>>>>
>>>> In the whole thing you may have to keep mn,da,yr as well depending on
>>>> whether the string is one or two or how you choose to treat it...I
>>>> simply used the time string for example...x==0 here, of course.
>>>>
>>>> --The dates in my excel file are formatted like this: 11/01/2002 14:37
>>> However, once I import this data into Matlab the same value is
>>> formatted lie this: '11/01/2002 2:37:35 PM' in which case the above
>>> code does not work. Any suggestions?
>> Use datenum() on the time string then pass that to datevec()
>>
>> Alternatively, if you have a current version of ML I believe datevec()
>> now has the option to specify a date/time format string. Which version
>> introduced that I don't know; it postdates mine so consult your
>> documentation.
>>
>> Alternatively, I don't know if there's a way to force the 24-hr format
>> when importing or not w/ some of the later versions rather than the
>> default of am/pm (which seems a silly thing to do imo, but TMW didn't
>> ask :) (me at least) ).
>>
>> --I have tried to specify a date/time format string with datevec,
>> but it does not seem to work.

Does your doc indicate that your version accepts it? I can't test it
but a quick look at the current doc's indicates it should be possible
(at least I think). What did you try specifically? Sample code is
always better than "didn't work" (just as we didn't get anywhere at all
until you provided a clear definition of what you expected).

>> you mean by using datenum() on the time string then passing it to
>> datevec(). I'm not sure that I follow. Both the date and time are
>> combined within a single cell of my series.

Exactly that...

 >> datevec(datenum('2:37:35 PM'))
ans =
          0 0 0 14.0000 37.0000 35.0000
 >> datevec(datenum('11/3/2002 2:37:35 PM'))
ans =
         2002 11 3 14 37 35
 >>

If your series are quite long it might be a performance boost to
separate out the date/time strings and work on only the time part, I
don't know. I've never used ML date stuff "in anger", only in small
stuff like this.

You can start off altogether and see if there's an issue, of course.

--

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
timeseries Leslie Wear 25 Nov, 2009 10:39:07
rssFeed for this Thread

Contact us at files@mathworks.com