How to reference a cell from a table within a table

37 次查看(过去 30 天)
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.

回答(2 个)

Peter Perkins
Peter Perkins 2023-7-17
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 2023-7-12
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 个评论
Aron Wiener
Aron Wiener 2023-7-12
移动:Voss 2023-7-12
In my case, however, the table has 57 different cells each with a table wtihin them.
Voss
Voss 2023-7-12
@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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by