Creating a daily date vector
조회 수: 7 (최근 30일)
이전 댓글 표시
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
채택된 답변
Steven Lord
2022년 4월 29일
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
Steven Lord
2022년 4월 29일
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)
추가 답변 (0개)
참고 항목
카테고리
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!