Nested tables with duplicate sub-column names?

63 views (last 30 days)
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
Stephen23
Stephen23 on 1 Mar 2021
@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{:})

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!