How to create nested tables

48 visualizaciones (últimos 30 días)
Darrian Low
Darrian Low el 25 de Nov. de 2025 a las 16:52
Comentada: Paul el 26 de Nov. de 2025 a las 13:19
I have the following code which creates a 3x3 table using some dummy data:
numsmall = [5; 3; 8];
nummedium = [26; 53; 81];
numbig = [316; 582; 711];
numtable = table(numsmall, nummedium, numbig);
I would like to store 'numtable' in another table, but I would like to also 'compress' it down to one element.
mastertable = table(numtable);
This results in the following:
However I would like to have '3x3 table' in the (1,1) box instead. In other words, I want to store that 3x3 table into that (1,1) box in my 'mastertable'.
I'm planning to put another 3x3 table into the (2,1) box when I receive that data in the future.
What changes do I need to make to my code?
  4 comentarios
Darrian Low
Darrian Low el 26 de Nov. de 2025 a las 0:13
I was looking to store numerous tables into another table or some other format to avoid having numerous tables in my workspace. The thought of having tables in a cell or structure didn't cross my mind!
Paul
Paul el 26 de Nov. de 2025 a las 13:19
Storing each table as an element of another container array makes it easy to access each table individually. If each tble has the same structure, then it's easy to combine them (or subsets of them) and perform operations on them using arrayfun/cellfun/structfun.
For example:
numsmall = [5; 3; 8];
nummedium = [26; 53; 81];
numbig = [316; 582; 711];
Store two tables with same format in a struct array
s(1).numtable = table(numsmall, nummedium, numbig);
s(2)=s(1);
Combine into a single table if we want to operate on all of the data
allT = vertcat(s.numtable)
allT = 6×3 table
numsmall nummedium numbig ________ _________ ______ 5 26 316 3 53 582 8 81 711 5 26 316 3 53 582 8 81 711
Use arrayfun to apply the same operation to each table. Using varfun here to get the sum of the columns in each table, but could be anything of use, e.g., plot
C = arrayfun(@(s) varfun(@sum,s.numtable),s,'Uni',false)
C = 1×2 cell array
{1×3 table} {1×3 table}
Combine those results if desired
sumT = vertcat(C{:})
sumT = 2×3 table
sum_numsmall sum_nummedium sum_numbig ____________ _____________ __________ 16 160 1609 16 160 1609
etc.

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 25 de Nov. de 2025 a las 17:47
Editada: dpb el 25 de Nov. de 2025 a las 19:11
numsmall = [5; 3; 8];
nummedium = [26; 53; 81];
numbig = [316; 582; 711];
numtable = table(numsmall, nummedium, numbig);
mastertable = table({numtable}) % store as a cell in another table
mastertable = table
Var1 ___________ {3×3 table}
You can dispense with the temporary...
clear mastertable % just to make it clear is new one
mastertable = table({table(numsmall, nummedium, numbig)})
mastertable = table
Var1 ___________ {3×3 table}
Remember you will have to dereference the cell with the {} to be able to address it.
mastertable.Var1
ans = 1×1 cell array
{3×3 table}
only returns the content of the cell which is a cell still...
mastertable.Var1{1}
ans = 3×3 table
numsmall nummedium numbig ________ _________ ______ 5 26 316 3 53 582 8 81 711
returns the content of the cell from which can then get the data.
mastertable.Var1{1}.numsmall
ans = 3×1
5 3 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by