How to use a loop with datetime and store the datetime values?
Show older comments
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
Accepted Answer
More Answers (2)
Peter Perkins
on 14 May 2018
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
Ameer Hamza
on 5 May 2018
The last line needs to be
dtm=[dtm{:}];
dpb
on 5 May 2018
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!!! :(
Ameer Hamza
on 5 May 2018
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.
dpb
on 5 May 2018
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.
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!