Rounding UTC times down help
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I'm trying to round times down to 10 mins at intervals 5, 15, 25, 35, 45, 55 mins. The format i'm using is yyyy-mm-dd hh:mm:ss in a table.
I have tried:
data_lables.UTC = datetime(data_lables.UTC);
vec = datevec(data_lables.UTC);
v5 = vec(:,5)+vec(:,6)/60;
vec(:,5) = round(v5/10)*10;
vec(:,6) = 0;
data_lables.UTC = datetime( vec );
but this rounds to 00, 10, 20 ,30 ,40 ect.
댓글 수: 5
dpb
2020년 6월 13일
The input to use retime has to be a timetable, true...
Alternatives would be far easier to try to write if saw what the data are and knew what you're trying to do with other data -- interpolate, bin, or just reset the time vector. If it's just the latter and they're even, roughly, then just writing the desired timestamp might be the simplest.
채택된 답변
Cris LaPierre
2020년 6월 13일
편집: Cris LaPierre
2020년 6월 13일
I'm not aware of a built-in way to do this. I'd try something like this
data_lables.UTC = datetime(data_lables.UTC);
% get date components
v = datevec(data_lables.UTC)
% Round minutes to previous 5 minute increments
v(:,5) = floor(v(:,5)/5)*5;
% Set seconds to zero
v(:,6)= 0;
% Update table varible
data_lables.UTC = datetime(v);
While it doesn't work for rounding to 5 minute increments, you might look into the dateshift function.
댓글 수: 2
Peter Perkins
2020년 6월 13일
Best to try to avoid mixing datetime with the older date/time functions (though in this case Chris's suggestion is fine). Use the datetime's properties:
min = data_lables.UTC.Minute;
data_lables.UTC.Minute = floor(min/5)*5;
data_lables.UTC.Second = 0;
As dpb says, if your times are the row times of a timetable, retime has you covered.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!