Declare a table where each column variable is an array of numbers
Show older comments
I am trying to set up a table where each column variable is not just a single number per row but an array of numbers. Here is an example:
varNames = {'10:00','10:15','10:30','10:45','11:00',};
varTypes = {'double','double','double','double','double'};
sz = [5 5];
T = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames,'RowNames',varNames);
What I would like is to have
T{1,1} = [1 2 3];
T{1,2} = [4 5 6];
T{1,3} = [7 8 9];
T{2,1} = [10 11 12];
etc.
When I try to populate it I get the following error:
T{1,1} = [1 2 3]
The value on the right-hand side of the assignment has the wrong width.
The assignment requires a value whose width is 1.
Accepted Answer
More Answers (1)
dpb
on 13 Feb 2021
Your table entries must be cells, not arrays--
>> t=table({1:3}) % NB: the cell array is the quantity for the table
t =
table
Var1
____________
{1×3 double}
>> t{2,1}={4:6} % add a new row -- again a cell
t =
2×1 table
Var1
____________
{1×3 double}
{1×3 double}
>>
Since are cells, don't have to have same number elements--
>> t{3,1}={4:8}
t =
3×1 table
Var1
____________
{1×3 double}
{1×3 double}
{1×5 double}
>>
altho the latter could make processing code complex.
You can even put the table as it is now inside--
>> t{4,1}={t}
t =
4×1 table
Var1
____________
{1×3 double}
{1×3 double}
{1×5 double}
{3×1 table }
>>
altho now referencing starts to get tricky! :)
>> t(4,1).Var1{:}.Var1(3,1)
ans =
1×1 cell array
{1×5 double}
>>
1 Comment
Martin Panevsky
on 13 Feb 2021
Categories
Find more on Structures 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!