How to create nested tables
Show older comments
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 Comments
Stephen23
on 25 Nov 2025
"What changes do I need to make to my code?"
Make that column a cell array, then you can store whatever you want in each cell.
Paul
on 25 Nov 2025
What is the benefit of nesting tables in cells in a table instead of putting the tables a cell or structure array?
Darrian Low
on 26 Nov 2025
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.
Accepted Answer
More Answers (0)
Categories
Find more on Tables 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!