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!