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

38 vues (au cours des 30 derniers jours)
Fox
Fox le 5 Mai 2018
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 commentaire
dpb
dpb le 5 Mai 2018
Modifié(e) : dpb le 5 Mai 2018
ML arrays begin at 1, not 0.
That's an interesting pattern to generate...have to ponder a couple minutes here.

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 5 Mai 2018
Modifié(e) : Ameer Hamza le 5 Mai 2018
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 commentaires
Ameer Hamza
Ameer Hamza le 5 Mai 2018
@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.
dpb
dpb le 5 Mai 2018
I can't think of a case it wouldn't be... :)

Connectez-vous pour commenter.

Plus de réponses (2)

Peter Perkins
Peter Perkins le 14 Mai 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

dpb
dpb le 5 Mai 2018
Modifié(e) : dpb le 5 Mai 2018
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 commentaires
Ameer Hamza
Ameer Hamza le 5 Mai 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
dpb le 5 Mai 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by