How to use a loop with datetime and store the datetime values?
조회 수: 29 (최근 30일)
이전 댓글 표시
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
채택된 답변
Ameer Hamza
2018년 5월 5일
편집: Ameer Hamza
2018년 5월 5일
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
Ameer Hamza
2018년 5월 5일
@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.
추가 답변 (2개)
Peter Perkins
2018년 5월 14일
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
댓글 수: 0
dpb
2018년 5월 5일
편집: dpb
2018년 5월 5일
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
Ameer Hamza
2018년 5월 5일
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
2018년 5월 5일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!