Timetable with string array

23 views (last 30 days)
vicsm
vicsm on 10 Oct 2019
Commented: Adam Danz on 10 Oct 2019
Hi,
I have a timetable (W) and when I run the following code
CM5_1min = retime(W,'minutely','mean');
I get this error:
Error using timetable/retime (line 140)
All variables in input timetables must be numeric, datetime, or duration when synchronizing using 'mean'.
Does anyone know how to solve the problem? Thank you!

Accepted Answer

Adam Danz
Adam Danz on 10 Oct 2019
Edited: Adam Danz on 10 Oct 2019
As the error indicates, you cannot retime non-numeric data. Your table contains a column "participant" which contains strings. You must remove that column before applying retime().
Here's a look at your raw data.
% Here's the first few rows of your table 'W'
head(W)
% date_time participant R G B lux
% ___________________ ___________ ____ ____ ____ ___
% 2018.05.19 08:56:00 "D" 8828 9756 6276 734
% 2018.05.19 08:57:00 "D" 12 4 4 0
% 2018.05.19 08:57:00 "D" 11 7 3 1
% 2018.05.19 08:57:00 "D" 75 39 17 3
% 2018.05.19 08:57:00 "D" 59 31 13 3
% 2018.05.19 08:57:00 "D" 258 122 56 10
% 2018.05.19 08:57:00 "D" 51 24 9 2
% 2018.05.19 08:58:00 "D" 570 193 88 19
% How many groups of participants are there?
unqParticipants = unique(W.participant)
% ans =
% 10×1 string array
% "D"
% "E"
% "F"
% "G"
% "P"
% "Q"
% "R"
% "S"
% "T"
% "V"
If you want to apply retime() across all of your data, ignoring the participant groups,
% Create a sub-table, remove 'participant'
Wnum = W;
Wnum.participant = [];
% apply retime() to the entire numeric timetable
CM5_1min = retime(Wnum,'minutely','mean');
%Check out the result
size(CM5_1min)
% ans = 18095 4
head(CM5_1min) %first few rows
% ans =
% 8×4 timetable
% date_time R G B lux
% ___________________ ______ ______ ______ ______
% 2018.05.14 09:50:00 1330 1151 453.5 91.5
% 2018.05.14 09:51:00 3192.8 2884.2 1164 228.33
% 2018.05.14 09:52:00 24439 21921 10705 1838.2
% 2018.05.14 09:53:00 29331 26110 12779 2194.8
% 2018.05.14 09:54:00 29041 25779 12505 2162.5
% 2018.05.14 09:55:00 16763 15040 7210.1 1225
% 2018.05.14 09:56:00 15675 13803 6534 1133.3
% 2018.05.14 09:57:00 14768 13287 6297.1 1083.9
If you want to apply retime() to each participan individually, you'll need to break apart the timetable by participant and store the minutely data in a cell array.
% Create a sub-table, remove 'participant'
Wnum = W;
Wnum.participant = [];
% separate minutely table for each participant
% This will take some several seconds....
unqParticipants = unique(W.participant)
TT = cell(numel(unqParticipants), 1);
for i = 1:numel(unqParticipants)
% identify rows of W that match participant i
rows = strcmp(W.participant, unqParticipants(i));
% retime for participant i
TT{i} = retime(Wnum(rows,:),'minutely','mean');
% If you want, you could add the participant back into the table
TT{i}.Participant = repmat(unqParticipants{i},size(TT{i},1),1);
end
% Check out the minutely table for participant n
n = 3
unqParticipants(n)
head(TT{n})
You can combine those minutely timetables back into one table and sort them by datetime
% Combine back to 1 TT
TTmain = sortrows(vertcat(TT{:}));
% confirm that the date_time column is monotonically increasing
all(diff(TTmain.date_time) >= 0) %should return 1 / true
  2 Comments
vicsm
vicsm on 10 Oct 2019
Thank you SO MUCH! This was exactly what I needed.
Adam Danz
Adam Danz on 10 Oct 2019
Glad I could help!

Sign in to comment.

More Answers (0)

Categories

Find more on Timetables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!