Creating a daily date vector
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Lejla Latifovic
le 29 Avr 2022
Modifié(e) : Lejla Latifovic
le 17 Mai 2022
Hello,
I hope this is a fairly easy question for the community. I have a half hourly datetime vector which I would like to convert to a daily datetime vector. I don't seem to be having any luck. There must be a simple code to do this? Any help would be much appreciated.
Here is an example of my data: date2017 (17520x1 datetime)
date2017 =
'01/01/2017 00:30:01'
'01/01/2017 01:00:01'
'01/01/2017 01:30:01'
'01/01/2017 02:00:01'
'01/01/2017 02:30:01'
'01/01/2017 03:00:01'
'01/01/2017 03:30:01'
'01/01/2017 04:00:01'
'01/01/2017 04:30:01'
'01/01/2017 05:00:01'
'01/01/2017 05:30:01'
'01/01/2017 06:00:01'
'01/01/2017 06:30:01'
'01/01/2017 07:00:01'
'01/01/2017 07:30:01'
'01/01/2017 08:00:01'
'01/01/2017 08:30:01'
'01/01/2017 09:00:01'
'01/01/2017 09:30:01'
'01/01/2017 10:00:01'
'01/01/2017 10:30:01'
'01/01/2017 11:00:01'
'01/01/2017 11:30:01'
'01/01/2017 12:00:01'
'01/01/2017 12:30:01'
'01/01/2017 13:00:01'
'01/01/2017 13:30:01'
'01/01/2017 14:00:01'
'01/01/2017 14:30:01'
I'd like to do this so that I can match it to daily mean values I have for temperature.
I tried this but something went wrong. I ended up with a DateDaily (367x0 timetable) vector, so no date values.
T = table(date2017, 'VariableNames' , {'Date'});
D = table2timetable(T);
DateDaily = retime(D, 'daily');
Thank you!
0 commentaires
Réponse acceptée
Steven Lord
le 29 Avr 2022
Based on your stated goal, I'd probably store the data in a timetable array and then use retime to change it to a 'daily' timetable.
n = 15;
randomHourVector = randi(6, n, 1); % n random integers between 1 and 6
d = datetime('now') + hours(cumsum(randomHourVector));
x = (1:n).';
tt = timetable(d, x)
tt2 = retime(tt, 'daily', @mean)
7 commentaires
Steven Lord
le 29 Avr 2022
The output of cumsum is not going to be suitable for use as an aggregation method in retime. It doesn't reduce a vector of data down to a scalar value like sum and mean do. But if I understand what you want, you can do that using cumsum after the retime call.
n = 15;
randomHourVector = randi(6, n, 1); % n random integers between 1 and 6
d = datetime('now') + hours(cumsum(randomHourVector));
x = (1:n).';
tt = timetable(d, x)
tt2 = retime(tt, 'daily', @mean)
tt2.CumulativeSum = cumsum(tt2.x)
Plus de réponses (0)
Voir également
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!