How to create nested tables
47 views (last 30 days)
Show older comments
Darrian Low
on 25 Nov 2025 at 16:52
Commented: Paul
on 26 Nov 2025 at 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 Comments
Paul
on 26 Nov 2025 at 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.
Accepted Answer
dpb
on 25 Nov 2025 at 17:47
Edited: dpb
on 25 Nov 2025 at 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 Comments
More Answers (0)
See Also
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!