split table in mat lab with respect to row values

26 views (last 30 days)
Hi i try to write script in matlab so as i can split my table in many others tables. my script return an error mentioned in the following picture which also clarify all my workspace and the description of my table(climSumry):
any one can help me to find where is the error in my script and how i should correct it ?? thank you. Regards

Answers (1)

Peter Perkins
Peter Perkins on 17 Feb 2018
Edited: Rena Berman on 16 Jul 2024
It's hard to know what you are trying to do, but I'm guessing you want to split up a table, by rows, based on one of the variables in the table. It looks like maybe you have two groups of rows.
I really recommend you take a look at the documentation for tables, including the section on subscripting, but to answer your question,
>> t = table(["a";"a";"a";"b";"b"],rand(5,1),rand(5,1))
t =
5×3 table
Var1 Var2 Var3
____ _______ _______
"a" 0.26187 0.10676
"a" 0.33536 0.65376
"a" 0.67973 0.49417
"b" 0.13655 0.77905
"b" 0.72123 0.71504
>> ta = t(t.Var1=="a",2:3)
ta =
3×2 table
Var2 Var3
_______ _______
0.26187 0.10676
0.33536 0.65376
0.67973 0.49417
>> tb = t(t.Var1=="b",2:3)
tb =
2×2 table
Var2 Var3
_______ _______
0.13655 0.77905
0.72123 0.71504
  2 Comments
Imola Fodor
Imola Fodor on 4 Aug 2021
is there an programatic way to do this? i have a lot of groups, and they are actually compound on 3 different columns
Peter Perkins
Peter Perkins on 6 Aug 2021
Here's one way: use findgroups to identify the groups in your table. Loop from 1:numGroups, and use something like
tables{i} = t(grp == i,2:3)
to put each subtable in a cell array.
BUT: I'm gonna suggest that you probably don't want to do that. Most of the things that you would do on those individual tables in the cell array can be done using groupsummary, groupfilter, grouptransform, rowfun, or maybe others.

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices 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!