How to create nested tables
조회 수: 48 (최근 30일)
이전 댓글 표시
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
Paul
2025년 11월 26일 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.
채택된 답변
dpb
2025년 11월 25일 17:47
편집: dpb
2025년 11월 25일 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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!