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 Comments

Use retime with desired duration vector...
Thank you, though i'm not sure how that would look in my case..
>> minutes(5:10:100)
ans =
1×10 duration array
5 min 15 min 25 min 35 min 45 min 55 min 65 min 75 min 85 min 95 min
>>
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)
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.

Sign in to comment.

 Accepted Answer

Cris LaPierre
Cris LaPierre on 13 Jun 2020
Edited: Cris LaPierre on 13 Jun 2020
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 Comments

Thank you to you both for your help! this code worked!
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.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!