Re: converting dates to intervals



Janis Weir wrote:
dpb <none@xxxxxxx> wrote in message <hek4h4$fnt$1@xxxxxxxxxxxxxxxxxxxxxxxxxx>...
Janis Weir wrote:
dpb <none@xxxxxxx> wrote in message <hek15v$fui$1@xxxxxxxxxxxxxxxxxxxxxxxxxx>...
Janis Weir wrote:
dpb <none@xxxxxxx> wrote in message <hejrod$sjg$1@xxxxxxxxxxxxxxxxxxxxxxxxxx>...
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.

--


.



Relevant Pages

  • Re: converting dates to intervals
    ... I don't want to consolidate to the nearest 5-min ... Put back into string w/ datestr ... if you have a current version of ML I believe datevec() now has the option to specify a date/time format string. ...
    (comp.soft-sys.matlab)
  • Re: converting dates to intervals
    ... I don't want to consolidate to the nearest 5-min ... Put back into string w/ datestr ... Use datenumon the time string then pass that to datevec() ... now has the option to specify a date/time format string. ...
    (comp.soft-sys.matlab)
  • Re: consolidating tables
    ... I'm attempting to consolidate multiple tables with similar ... by calling him Monkey Boy - he hated it. ... Public Function getFieldNames(tblName As String) As String ... "Build History") ...
    (comp.databases.ms-access)
  • Re: converting dates to intervals
    ... I don't want to consolidate to the nearest 5-min ... but rather to the interval within which the datetime ... Put back into string w/ datestr() ...
    (comp.soft-sys.matlab)
  • Re: bonehead date handling question
    ... Error using ==> datevec ... Cannot convert input into specified date string. ... MIT Lincoln Laboratory ... MATLAB FAQ: http://www.mit.edu/~pwb/cssm/ ...
    (comp.soft-sys.matlab)