split timetables into multiple hourly timetables and save as separate .mat files

10 views (last 30 days)
I have some timeseries sensor data that start at a random time and end at a random time (a sample shown below):
08/05/2019,16:24:37.988776000 0.464062500000000 4.68968700000000
08/05/2019,16:24:37.989776000 0.580312500000000 4.75656200000000
08/05/2019,17:24:37.990776000 0.679687500000000 3.00000000000000
08/05/2019,16:24:37.991776000 0.472500000000000 4.95500000000000
08/05/2019,18:24:37.992776000 0.670312500000000 4.75718800000000
I am trying to split the timetable into separate timetables for each hour of data. I've used the discretise function and created a bin column based on the hours each row of data corresponds to using the script:
test = load([dir_file,test_name,'.mat']);
ttab = test.test_timetable;
binned = discretize(ttab.Var1,'Hour');
tab_binned = table(ttab.Var1,ttab.X,binned);
tab_binned.Properties.VariableNames = {'var2','var1','binned'};
ttab_c_binned = [ttab,tab_binned(:,{'binned'})];
I could use for loops and split the time table and save separate time tables per hour but I wondered if there was a neater way of doing this?
Thanks!

Accepted Answer

Peter Perkins
Peter Perkins on 19 Jun 2019
You can do this in several ways, but honestly the most straight-forward is to write a loop, as shown below. The real question is, why do you want to do this? I'm genuinely asking, because there are tools that let you do things like hourly means without splitting the data up.
Anyways, my solution (which could certainly be tightened up but is at least clear):
>> tt = array2timetable(rand(10,2),'RowTimes',datetime(2019,6,19)+3*hours(sort(rand(10,1))))
tt =
10×2 timetable
Time Var1 Var2
____________________ _________ _______
19-Jun-2019 00:13:40 0.10665 0.43141
19-Jun-2019 00:22:11 0.9619 0.91065
19-Jun-2019 00:33:06 0.0046342 0.18185
19-Jun-2019 00:43:11 0.77491 0.2638
19-Jun-2019 00:43:11 0.8173 0.14554
19-Jun-2019 01:03:10 0.86869 0.13607
19-Jun-2019 01:12:19 0.084436 0.86929
19-Jun-2019 01:32:23 0.39978 0.5797
19-Jun-2019 01:51:58 0.25987 0.54986
19-Jun-2019 02:33:32 0.80007 0.14495
>> h = unique(tt.Time.Hour)
h =
0
1
2
>> c = cell(length(h),1);
>> for i = 1:length(c), c{i} = tt(tt.Time.Hour == h(i),:); end
>> c{:}
ans =
5×2 timetable
Time Var1 Var2
____________________ _________ _______
19-Jun-2019 00:13:40 0.10665 0.43141
19-Jun-2019 00:22:11 0.9619 0.91065
19-Jun-2019 00:33:06 0.0046342 0.18185
19-Jun-2019 00:43:11 0.77491 0.2638
19-Jun-2019 00:43:11 0.8173 0.14554
ans =
4×2 timetable
Time Var1 Var2
____________________ ________ _______
19-Jun-2019 01:03:10 0.86869 0.13607
19-Jun-2019 01:12:19 0.084436 0.86929
19-Jun-2019 01:32:23 0.39978 0.5797
19-Jun-2019 01:51:58 0.25987 0.54986
ans =
1×2 timetable
Time Var1 Var2
____________________ _______ _______
19-Jun-2019 02:33:32 0.80007 0.14495

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!