Rounding UTC times down help

조회 수: 2 (최근 30일)
Alistair Walker
Alistair Walker 2020년 6월 13일
댓글: Peter Perkins 2020년 6월 13일
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
Alistair Walker
Alistair Walker 2020년 6월 13일
though when I do this I get the error
(>> TT2 = retime(data_lables.UTC,'regular','linear','TimeStep',dt) Undefined function 'retime' for input arguments of type 'datetime'.)
the code im using is :
data_lables.UTC = datetime(data_lables.UTC);
t1=data_lables.UTC(1:1) %start time 12-Nov-2019 16:26:35
t2=data_lables.UTC(length(data_lables.UTC))%end time 22-Nov-2019 08:05:44
t11=datevec(datenum(t1));
t22=datevec(datenum(t2));
duration= etime(t22,t11)%time_interval_in_seconds
duration_min=duration/60
dt = minutes(5:10:duration_min);
TT2 = retime(data_lables.UTC,'regular','linear','TimeStep',dt)
dpb
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
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
Alistair Walker
Alistair Walker 2020년 6월 13일
Thank you to you both for your help! this code worked!
Peter Perkins
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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by