Timetable with string array
17 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Adam Danz
2019-10-10
编辑:Adam Danz
2019-10-10
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Timetables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!