How to create nested tables
48 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
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)
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)
Combine those results if desired
sumT = vertcat(C{:})
etc.
Respuesta aceptada
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
You can dispense with the temporary...
clear mastertable % just to make it clear is new one
mastertable = table({table(numsmall, nummedium, numbig)})
Remember you will have to dereference the cell with the {} to be able to address it.
mastertable.Var1
only returns the content of the cell which is a cell still...
mastertable.Var1{1}
returns the content of the cell from which can then get the data.
mastertable.Var1{1}.numsmall
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!