How to use a loop with datetime and store the datetime values?

Hello everybody,
I want to create a time vector (datetime), that consists of the time span 02-Jan-2001-18-Apr-2012. Here every day should be sampled in 5 min steps from 9.35-16.00.
Example:
'02-Jan-2001 09:35:00'
'02-Jan-2001 09:40:00'
'02-Jan-2001 09:45:00'
...
'02-Jan-2001 16:00:00'
'03-Jan-2001 9:35:00'
...
'03-Jan-2001 16:00:00'
...
'18-Apr-2012 16:00:00'
I have 4124 days to create with 78 five minute steps per day. I already constructed the code to create the 5minute time steps for one day.
t1 = datetime(2001,01,2,9,35,0);
t2 = datetime(2001,01,2,16,0,0);
t = (t1:minutes(5):t2)';
However I don't know how to create the datetime for the whole time span 2001-2012. I tried to use a loop, in the following form:
for i=0:4124
t(i) = (t1:minutes(5):t2)+days(i);
end
However I get an error message, it seems to be not possible to store the created values in the way t(i), because I am using datetime here and not a double format.
Can anybody help me with this problem ?

1 Comment

ML arrays begin at 1, not 0.
That's an interesting pattern to generate...have to ponder a couple minutes here.

Sign in to comment.

 Accepted Answer

Here is a solution without for loop
T1 = datetime('02-Jan-2001');
T2 = datetime('18-Apr-2012');
totalNumDays = days(T2-T1);
t1 = datetime(2001,01,2,9,35,0);
t2 = datetime(2001,01,2,16,0,0);
oneDay = (t1:minutes(5):t2);
complete = arrayfun(@(x) oneDay+x, 0:totalNumDays, 'UniformOutput', false);
completeList = [complete{:}];

5 Comments

Thanks Hamza. It's working fine for my purposes.
Probably more convenient to cast as a single column vector of datetimes, but good use of arrayfun with the integer...I thought of trying (but didn't) as ISTR already finding out that arrayfun does accept datetime arrays I wanted to augment by and there is no equivalent that does work with datetime arrays (which is probably and enhancement request waiting to be submitted).
@dpb thanks for your comment. I have edited the answer to convert the result to a linear list as that might be more useful in some cases.
I can't think of a case it wouldn't be... :)

Sign in to comment.

More Answers (2)

I'm a bit late to this party, but isn't it just this?
>> dt = datetime(2001,1,2:5);
>> et = hours(9:12)';
>> t = repmat(dt,length(et),1) + repmat(et,1,length(dt))
t =
4×4 datetime array
02-Jan-2001 09:00:00 03-Jan-2001 09:00:00 04-Jan-2001 09:00:00 05-Jan-2001 09:00:00
02-Jan-2001 10:00:00 03-Jan-2001 10:00:00 04-Jan-2001 10:00:00 05-Jan-2001 10:00:00
02-Jan-2001 11:00:00 03-Jan-2001 11:00:00 04-Jan-2001 11:00:00 05-Jan-2001 11:00:00
02-Jan-2001 12:00:00 03-Jan-2001 12:00:00 04-Jan-2001 12:00:00 05-Jan-2001 12:00:00
>> t = t(:)
t =
16×1 datetime array
02-Jan-2001 09:00:00
02-Jan-2001 10:00:00
02-Jan-2001 11:00:00
02-Jan-2001 12:00:00
03-Jan-2001 09:00:00
03-Jan-2001 10:00:00
03-Jan-2001 11:00:00
03-Jan-2001 12:00:00
04-Jan-2001 09:00:00
04-Jan-2001 10:00:00
04-Jan-2001 11:00:00
04-Jan-2001 12:00:00
05-Jan-2001 09:00:00
05-Jan-2001 10:00:00
05-Jan-2001 11:00:00
05-Jan-2001 12:00:00
dtday=[t1:datetime(2012,4,18+1)].'; % array of fixed start times each day
dtm=[];
for i=1:length(dtday)
dtm=[dtm;[dtday(i)+minutes(0:5:389)].']; % add the time vector duration for each day
end
>> [dtm(1) dtm(end)]
ans =
datetime
02-Jan-2001 09:35:00 18-Apr-2012 16:00:00
>>
Above needs optimization to compute and preallocate output array but shows at least one way...
ADDENDUM Well, hadn't tried since upgraded past R2012b I guess...
dtday=[t1:datetime(2012,4,18+1)].'; % array of fixed start times
dur=minutes(0:5:389).'; % the daily minutes as duration array
dtm=arrayfun(@(t) t+dur,dtday,'uniform',0);
dtm=dtm{:};
does work equivalently to other solution excepting cast to a full datetime native array instead of leaving a cell array of datetime arrays.

4 Comments

Well, no, that's not right (quite), either; that returns a 2D array, what need is
dtm=[dtm{:}]; dtm=dtm(:);
to end up with the column vector of datetime. cell2mat not being object aware is a pit(proverbial)a(ppendage).
Illustration for just a couple elements--
>> t=[arrayfun(@(t) t+dur,dtday(1:2),'uniform',0)];
>> whos t
Name Size Bytes Class Attributes
t 2x1 2722 cell
>> tt=t{:}; % try to dereference only end up with
>> whos tt
Name Size Bytes Class Attributes
tt 78x1 1249 datetime
>>
Only end up with one of the two cells and it's the first; I'm not quite sure just why colon here doesn't behave as I'd expected...but end up with only the first???
Your syntax
>> tt=[t{:}];
>> whos tt
Name Size Bytes Class Attributes
tt 78x2 2497 datetime
>>
does return both but it's 2D array, still not what really want.
>> tt=cell2mat(t);
Error using cell2mat (line 52)
CELL2MAT does not support cell arrays containing cell arrays or objects.
>>
That's a kick in the gut...just plain rude and oversight there's no equivalent for datetime objects.
>> tt=[t{:}]; tt=tt(:);
>> whos tt
Name Size Bytes Class Attributes
tt 156x1 1249 datetime
>>
is the end result needed; or use
>> tt=reshape([t{:}],[],1);
>> whos tt
Name Size Bytes Class Attributes
tt 156x1 2497 datetime
>>
to avoid the two-statement need since there is no trailing (:) syntax for two-level addressing here.
All in all, a lot of work to get it recast; I hate cell arrays!!! :(
Yes, I noticed that it create a 2D array when I commented that. But I didn't mention it because conversion to a column vector is quite trivial.
It is also interesting to mention that you can avoid all this conversion to a single column at the end if you initialize dur as a row vector instead of a column. Try this
t1 = datetime(2001,01,2,9,35,0);
dtday=[t1:datetime(2012,4,18+1)].'; % array of fixed start times
dur=minutes(0:5:389); % the daily minutes as duration array
dtm=arrayfun(@(t) t+dur,dtday,'uniform',0);
dtm2=[dtm{:}]';
it will also return a 1D array.
Hmmm...and I went to the trouble to create dur as a column specifically because I wanted end result as column! :) As noted, I tend to avoid cell arrays like the plague unless absolutely mandatory so hadn't ever really observed the specific behavior. Seems more than peculiar, but guess once aware can (if can remember to) use the other paradigm...thanks for pointing it out.

Sign in to comment.

Categories

Asked:

Fox
on 5 May 2018

Answered:

on 14 May 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!