Sorting and Re-arranging a text file using Matlab by a specific column

I'm a newbie of Matlab world and learning a lot from this forum!!
But, I couldn't find the answer for my question at this moment.
"What I have in a text file"
  • First row is a "Text" form
  • Second to End is a "Number"
  • Example in 10 rows by 3 column (and would like to expand over 1000 rows and column eventually)
Time ID Number
1 1 2
2 2 3
3 3 8
4 1 3
5 2 4
6 3 4
7 2 5
8 3 1
9 1 3
.........
"What I want to do"
  • First, sort my text file by 'Second' column (called ID in my example)
  • Second, re-arranging it by ID as it should be appeared as,
  • Number of each ID may not be even, so I need some idea to cut the data at specific row at each ID.
Time ID Number
1 1 2
4 1 3
9 1 3
2 2 3
5 2 4
7 2 5
3 3 8
6 3 4
8 3 1
.........
  • Then, I want to find the 'Max. value' and 'Sum' for each ID in a Text or as an output in Matlab as,
ID Max Sum
1 3 8
2 5 12
3 8 13
Your HELP will be very very appreciated!!!

Answers (1)

T = readtable('test.txt')
T = 9×3 table
Time ID Number ____ __ ______ 1 1 2 2 2 3 3 3 8 4 1 3 5 2 4 6 3 4 7 2 5 8 3 1 9 1 3
T = sortrows(T,'ID')
T = 9×3 table
Time ID Number ____ __ ______ 1 1 2 4 1 3 9 1 3 2 2 3 5 2 4 7 2 5 3 3 8 6 3 4 8 3 1
G = findgroups(T.ID);
S = groupsummary(T,"ID",{"max","sum"},"Number");
S = removevars(S,'GroupCount');
S = renamevars(S,["max_Number","sum_Number"],["max","sum"])
S = 3×3 table
ID max sum __ ___ ___ 1 3 8 2 5 12 3 8 13
writetable(S,'summary.txt')

Categories

Asked:

on 3 Feb 2022

Edited:

on 3 Feb 2022

Community Treasure Hunt

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

Start Hunting!