Clear Filters
Clear Filters

How to reference a cell from a table within a table

55 views (last 30 days)
Hi super quick question. I have a table with another set of tables within it read from a folder. I want to plot certain rows from the table within the table, but how can I reference this in Matlab?
Thank you for any help.

Answers (2)

Peter Perkins
Peter Perkins on 17 Jul 2023
Aron, it sounds like you have tables in a cell array in a table. Dirk is assuming you have "nested tables", which is different but also useful. Assuming you have something like this
t = table({table(rand(5,1),rand(5,1));table(rand(5,1),rand(5,1));table(rand(5,1),rand(5,1))})
t = 3×1 table
Var1 ___________ {5×2 table} {5×2 table} {5×2 table}
you should ask yourself how you would get at those tables if the cell array were in your workspace. You'd use braces, right? S ame thing here, only on the var in the table
t.Var1{1}
ans = 5×2 table
Var1 Var2 _______ _______ 0.55235 0.37883 0.91089 0.68718 0.66269 0.61634 0.36953 0.46662 0.356 0.95732
t.Var1{2}(1:2,:)
ans = 2×2 table
Var1 Var2 _______ ________ 0.79442 0.68389 0.9692 0.098918
Whether nested tables or tables in a cell array in a table are the right thing to use, I can't say. Both are useful, just for different purposes.

Dirk Engel
Dirk Engel on 12 Jul 2023
You can access a specific inner table by its variable name. Consider the following table with two inner tables.
t = table(table(rand(5,1), rand(5,1)), table(rand(5,1)), 'VariableNames', {'InnerTable1', 'InnerTable2'})
t = 5×2 table
InnerTable1 InnerTable2 Var1 Var2 Var1 __________________ ___________ 0.9087 0.5988 0.17796 0.72553 0.69322 0.3135 0.91621 0.43767 0.12761 0.4482 0.11312 0.62554 0.55468 0.50289 0.37345
To access row 3 of InnerTable1, write
t.InnerTable1(3, :)
However, you can also simply write
t(3, 1)
where column index 1 refers to the outer table's first variable, which here is 'InnerTable1'.
  2 Comments
Aron Wiener
Aron Wiener on 12 Jul 2023
Moved: Voss on 12 Jul 2023
In my case, however, the table has 57 different cells each with a table wtihin them.
Voss
Voss on 12 Jul 2023
@Aron Wiener: Maybe it'd be convenient to use varfun in your case.
t = table(table(rand(5,1), rand(5,1)), table(rand(5,1)), 'VariableNames', {'InnerTable1', 'InnerTable2'})
t = 5×2 table
InnerTable1 InnerTable2 Var1 Var2 Var1 _____________________ ___________ 0.5781 0.3214 0.05468 0.055876 0.0023291 0.013252 0.57597 0.63605 0.99085 0.20938 0.10417 0.44687 0.86833 0.1974 0.16186
temp = varfun(@(t)t{3,:},t)
temp = 1×2 table
Fun_InnerTable1 Fun_InnerTable2 __________________ _______________ 0.57597 0.63605 0.99085
data = temp{:,:}
data = 1×3
0.5760 0.6360 0.9908

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!