Can I use 'table' as a variable type in a table
Show older comments
Help for 'table' says that I can use 'table' as a variable type in a table. I receive the error msg;
"The value on the right-hand side of the assignment has the wrong width. The assignment requires a value whose width is 0"
when trying to run the following code.
InnerTable = table('Size',[1 2],'VariableTypes',{'double', 'double'});
InnerTable{2,1} = 14.34;
InnerTable{3,2} = 45;
Table = table('Size',[1 3],'VariableTypes',{'table', 'double', 'struct'});
Table{1,1} = InnerTable;
Can a table be assigned to a location within a table? How?
5 Comments
I am having difficulty getting further than this:
Table = table('Size',[1 3],'VariableTypes',{'table', 'double', 'struct'});
T = table(1);
Table{1,1} = T(:,[])
Voss showed below how this can be done by refering to the entire nested table at once. It is also possible to use indexing into just part of the nested table (but remember that both the LHS and RHS tables must have the same variables/columns when indexing like this):
T1 = table('Size',[3,2], 'VariableTypes',{'double','double'});
T1{2,1} = 14.34;
T1{3,2} = 45
T0 = table('Size',[5,3], 'VariableTypes',{'table','double','struct'})
T0.Var1.Var1 = nan(5,1);
T0.Var1.Var2 = nan(5,1) % ensure the same variables/columns
T0.Var1(2:4,:) = T1
As well as the immediate problem of non-matching variables/columns, your code will also have the problem that you are trying to force a 3-row table into a one row table, which will not work. If you want to store a non-singular-row array in one row of a table then you will need to use a container array, e.g. a cell array.
Ronald Stahl
on 26 Feb 2025
"I was expecting (hoping) that a table of any size could be assigned to a single cell in another table. Clearly not. Time to approach my problem another way."
Tables do not have "cells". Every variable/column of a table is simply an array, which means there is absolutely nothing stopping you from using a cell array as one of those columns, if you need. Then you can allocate a nested table of any size to that variable (i.e. cell array) of the parent table. But do not confuse tables themselves with "cells" (which they definitely do not have).
Just to illustrate what Stephen23 and Voss have (I think) already said:
This is what's usually referred to as "nested tables"
t1 = table([1;2;3],[4;5;6],VariableNames=["V1" "V2"]);
t2 = table([7;8;9],[10;11;12],VariableNames=["V3" "V4"]);
x = [13;14;15];
t = table(t1,t2,x)
The table t contains three variables, two of which are themselves tables. The third variable in t is an ordinary double. The key thing to notice is that t1, t2, and x all have the same number of rows -- they are all "aligned". This kind of nested tables is mostly about grouping variables -- V1 and V2 are "together", as are V3 and V4.
Here's another way to have tables inside a table
t1 = table([1;2;3],[4;5;6],VariableNames=["V1" "V2"]);
t2 = table([7;8;9;10],[11;12;13;14],VariableNames=["V3" "V4"]);
t3 = table(Size=[0 2],VariableTypes=["double" "double"],VariableNames=["V3" "V4"]);
x = [15;16;17];
t = table({t1;t2;t3},x)
Notice that t1, t2, and t3 have different numbers of rows (and t3 is empty). This is what you do to have tables of different sizes inside a table. t.Var1 is a cell array
t.Var1
and t.Var1{1} is t1
t.Var1{1}
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!