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
dpb 2020 年 6 月 13 日
Use retime with desired duration vector...
Alistair Walker
Alistair Walker 2020 年 6 月 13 日
Thank you, though i'm not sure how that would look in my case..
dpb
dpb 2020 年 6 月 13 日
>> 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
>>
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 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeTimetables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by