Nested tables with duplicate sub-column names?

I'm trying to create a table that consists of two sub-tables, with different names, each of which is two columns wide. All four sub-columns have the same number of rows. The issue is that the first sub-column in both sub-tables has the same name, and ditto for the second:
----- START ----- ----- STOP -----
Week Seconds Week Seconds
1234 567890 1234 604799
1235 123450 1235 271828
When I try to nest this into a single table, as shown a few of the examples here, I keep getting "Duplicate table variable name: 'Week'."
Of course I could rename the columns as something like "WeekStart", "SecondsStart", etc. to avoid this, but that's a bit awkward—in that case I'd just make a single four-column table, but would lose the nice grouping in the process. I've tried a few of the examples above, as well as mergevars and splitvars, but no luck so far.
Is there a clean way to do this, or is this just not how MATLAB tables are meant to be used?

 Accepted Answer

Stephen23
Stephen23 on 3 Jun 2020
Edited: Stephen23 on 25 May 2022
The link you gave is unrelated to nested tables: the goal there was to (in some way) merge multiple tables into one table.
It is certainly possible to create nested tables:
>> T1 = array2table([1234,567890;1235,123450])
T1 =
Var1 Var2
____ ______
1234 567890
1235 123450
>> T2 = array2table([1234,604799;1235,271828])
T2 =
Var1 Var2
____ ______
1234 604799
1235 271828
>> TP = table(T1,T2) % create nested table!
TP =
T1 T2
___________ ___________
[1x2 table] [1x2 table]
[1x2 table] [1x2 table]
>> TP.T1.Var1
ans =
1234
1235
>> TP.T1.Var2
ans =
567890
123450
"Is there a clean way to do this..."
Not really: accessing the data is more complex, which means more bugs, more time to write and maintain, slower, etc.
"...or is this just not how MATLAB tables are meant to be used?"
Ultimately nested tables are unlikely to be very useful: none of the inbuilt tools for processing table data will work on them.
UPDATE: sometime since I wrote this answer MATLAB changed to also display the column/variable names of the nested tables, for example see https://www.mathworks.com/matlabcentral/answers/1726625-can-you-have-a-multilevel-table

7 Comments

I was imprecise in using the term "nested." The "Merged Variable as Table" example on the MERGEVARS help page is closest to what I had in mind, in that the three rightmost columns (Cause, Loss, RestorationTime) get grouped under a super-header (LossData).
The fundamental issue, if I've understood your answer correctly, is that it is forbidden to add any other columns named Cause, Loss, RestorationTime, Region, OutageTime, or Customers to this table, regardless of whether they are at the top level or grouped together underneath a super-header.
I hadn't worked with MATLAB tables before, so thanks for clarifying. Cheers.
Tables are a container class, i.e. they contain instances of other arrays. Those other arrays can be of any class.
When variables are merged, those other arrays are concatenated together. We can see this in the first example in the mergevars documentation which shows two separate 3x1 numeric arrays being concatenated together into one 3x2 numeric array, which is contained in Var2:
T2=3×3 table
A Var2 D
_ __________ _____
1 5 3.14 {'a'}
2 11 2.72 {'b'}
3 12 1.37 {'c'}
Notice that the two columns of that 3x2 numeric array do NOT have their own headers/column names (like you are asking about), simply because numeric arrays do not have headers/column names. It should be clear by now that if we were to require two separate column names for the data contained in Var2 in the above example, then we cannot use a 3x2 numeric array to store that data (because numeric arrays do not have column names).
Question: what data class has column names? Answer: tables.
So if we need to use column names for each individual column of Var2 in that example, then we would have to use nested tables.
"...it is forbidden to add any other columns named ... regardless of whether they are at the top level or grouped together underneath a super-header."
Duplicate names are clearly not allowed in one table, and the table class is not designed for arbitrary nesting of variable names within one table (you could probably write your own class for that, but it would not be a trivial task).
I was trying to find out how to nest tables AND access them so this was very useful, thank you Stephen. Since we can access data in that way (i.e. TP.T1.Var2), what exactly do you mean by "...none of the inbuilt tools for processing table data will work on them." Isn't the dot notation an inbuilt table tool?
I'm somewhat new to MATLAB so if you happen to answer this, please keep that in mind - thank you.
"Isn't the dot notation an inbuilt table tool? "
I wouldn't count just accessing data as an "inbuilt table tool". I was more referring to this kind of processing:
as well as almost all of the functions listed here:
Dear Stephen Cobeldick,
is there a possibilty to concatenate the nested table in a for-loop without nesting it more and more with every repitition?
I am creating a new table in an inner loop and would like to achieve a structure equivalent to this:
TP = table(T1,T2,T3,...)
Since I have to renew the table after every repitition of the inner loop I tried:
TP = table('Size',[8 1],'VariableTypes',"cell");
...
TP = table(TP,T1);
Unfortunately it gets more and more nested with every repitition.
I appreciate the help!
Best regards
Pierre
@Pierre Bolz: note that the table command places its input arguments as columns/variables of the output table, i.e. as contents in the output table, so on each loop iteration your code nests the previous table inside a new table.
The simple solution is to add a new variable on each loop iteration:
TP = table();
TP.('table1') = table();
TP.('table2') = table();
...
Or you could preallocate a cell array, assign the tables to each cell, and call table once after the loop:
N = number of iterations
C = cell(1,N);
for k = 1:N
C{k} = table(..);
end
T = table(C{:})
Thank you very much!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Tags

Asked:

AMM
on 3 Jun 2020

Edited:

on 25 May 2022

Community Treasure Hunt

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

Start Hunting!