MATLAB: How to split tables and extract all data based on a column value

table

Hi all. I'm new to MATLAB and coding in general. I'm practicing on this data set (r_T.mat).
I want to create separate tables based on values on column 'G'. These tables need to include all data (from column 'custNo' up to column 'p').
So the output should be 3 separate tables with all data still intact.
I can't seem to separate them will all data still intact. When I apply
r_T.G(r_T.G == 1)
it just outputs
ans =
1
1
1
1
1
and it does NOT extract ALL data from column 'custNo' up to column 'p' in a separate table.
To provide more context, my end goal is to run a while loop to get the sum of column 'd' up until <= a variable 'C' for each group in column 'G'.
Any help or tips is greatly appreciated. Thank you.

Best Answer

  • See this page on how to access data in a table.
    You are close. Don't forget to include both row and column indices, and make the outer variable the data you want to extract. In this case, that is probably the table name only.
    load r_T.mat
    r_T(r_T.G == 1,:)
    ans = 5x8 table
    custNo x_coor y_coor d nearest_dist m G p ______ ______ ______ __ ____________ _ _ ______ 1 13 6 15 11.18 2 1 1.3416 2 18 16 30 0 2 1 100 3 13 6 15 11.18 2 1 1.3416 6 16 9 13 7.2801 2 1 1.7857 7 14 17 20 4.1231 2 1 4.8507
    Of course, if it's just the sum you ultimately want, try using groupsummary.
    sumTbl = groupsummary(r_T,'G','sum','d')
    sumTbl = 3x3 table
    G GroupCount sum_d _ __________ _____ 1 5 93 2 1 30 3 4 68