Declare a table where each column variable is an array of numbers

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.

 採用された回答

Adam Danz
Adam Danz 2021 年 2 月 13 日
varNames = {'10:00','10:15','10:30','10:45','11:00',};
varTypes = {'double','double','double','double','double'};
sz = [5,5];
T = cell2table(repmat({nan(1,3)},sz),'VariableNames',varNames,'RowNames',varNames)
T = 5x5 table
10:00 10:15 10:30 10:45 11:00 _________________ _________________ _________________ _________________ _________________ 10:00 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10:15 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10:30 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10:45 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 11:00 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
T{1,1} = [1,2,3]
T = 5x5 table
10:00 10:15 10:30 10:45 11:00 _________________ _________________ _________________ _________________ _________________ 10:00 1 2 3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10:15 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10:30 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10:45 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 11:00 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN

4 件のコメント

Martin Panevsky
Martin Panevsky 2021 年 2 月 13 日
That works, thank you very much.
Martin Panevsky
Martin Panevsky 2021 年 2 月 13 日
Also, is there a way to index an individual table with say T(1), T(2), etc. where each one is an individual table ? In other words, how to create an array of tables ?
You'll have to store them in a cell array.
TA = {table((1:3)'), table((100:200)'), table(["A";"B"])};
TA{1}
ans = 3x1 table
Var1 ____ 1 2 3
TA{1}.Var1
ans = 3×1
1 2 3
Martin Panevsky
Martin Panevsky 2021 年 2 月 14 日
Thank you, I figured that out, thanks.

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2021 年 2 月 13 日

0 投票

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 件のコメント

Martin Panevsky
Martin Panevsky 2021 年 2 月 13 日
Thank you, that works too and directly uses the table command.
Thanks.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by