How can i filter a table with several variables

61 views (last 30 days)
Hey guys,
i have a table with four rows: String, String, Datetime and Double. I want to generate a new table with all four rows filtered by a startdate and a enddate that i can analyse for example just 10 years (2000-2010).
Thank you :)
  1 Comment
MarKf
MarKf on 3 Jan 2023
%% sample data
dates = datetime - years(5:5:40)' + hours(randn(8,1));
doublesx = randn(8,1);
strings1 = ('a':char('a'+7))';
strings2 = ('m':char('m'+7))';
example_table = table(strings1,strings2,dates,doublesx)
example_table = 8×4 table
strings1 strings2 dates doublesx ________ ________ ____________________ ________ a m 03-Jan-2018 10:09:18 -0.92048 b n 03-Jan-2013 04:37:03 0.25715 c o 04-Jan-2008 00:52:16 0.79814 d p 03-Jan-2003 18:22:47 -0.26506 e q 03-Jan-1998 14:05:05 1.3091 f r 03-Jan-1993 11:11:26 -1.5165 g s 04-Jan-1988 04:03:51 0.74112 h t 03-Jan-1983 23:47:08 -1.3076
%% new table selct
idx = (example_table.dates.Year >= 2000) & (example_table.dates.Year < 2010);
new_table = example_table(idx,:) %even if 3rd position dates
new_table = 2×4 table
strings1 strings2 dates doublesx ________ ________ ____________________ ________ c o 04-Jan-2008 00:52:16 0.79814 d p 03-Jan-2003 18:22:47 -0.26506
P.S. Actual or faux example table .mat file or generating code shared would be appreaciated and make it easier to help. You likely mean for example 4 colums and many row entries, but in case you do have a horizontal table it'd work all the same. Also the number of variables does not matter. You could also convert it to a timetable using table2timetable and then use timerange

Sign in to comment.

Accepted Answer

Voss
Voss on 3 Jan 2023
Edited: Voss on 3 Jan 2023
% an example table with four columns:
dt = datetime(2023,1,(1:10).');
A = string(rand(10,1));
B = string(rand(10,1));
C = rand(10,1);
t = table(A,B,dt,C)
t = 10×4 table
A B dt C __________ __________ ___________ ________ "0.96935" "0.017237" 01-Jan-2023 0.42444 "0.92083" "0.17279" 02-Jan-2023 0.39157 "0.054817" "0.93052" 03-Jan-2023 0.1114 "0.21611" "0.74939" 04-Jan-2023 0.1513 "0.36615" "0.32896" 05-Jan-2023 0.51285 "0.56422" "0.20432" 06-Jan-2023 0.68134 "0.53045" "0.92401" 07-Jan-2023 0.062899 "0.84537" "0.70987" 08-Jan-2023 0.35308 "0.18631" "0.23875" 09-Jan-2023 0.71427 "0.13705" "0.38498" 10-Jan-2023 0.34559
% a logical array to be used as a filter (keep data from dates between
% Jan 3, 2023 and Jan 7, 2023, inclusive):
startDate = datetime(2023,1,3);
endDate = datetime(2023,1,7);
idx = t{:,3} >= startDate & t{:,3} <= endDate
idx = 10×1 logical array
0 0 1 1 1 1 1 0 0 0
% create the new table from just those dates:
new_t = t(idx,:)
new_t = 5×4 table
A B dt C __________ _________ ___________ ________ "0.054817" "0.93052" 03-Jan-2023 0.1114 "0.21611" "0.74939" 04-Jan-2023 0.1513 "0.36615" "0.32896" 05-Jan-2023 0.51285 "0.56422" "0.20432" 06-Jan-2023 0.68134 "0.53045" "0.92401" 07-Jan-2023 0.062899

More Answers (1)

Steven Lord
Steven Lord on 3 Jan 2023
If you turned your table into a timetable using table2timetable you could use a timerange to index into the rows of that timetable.

Categories

Find more on Interactive Control and Callbacks 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!