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.

 채택된 답변

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

That works, thank you very much.
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
Thank you, I figured that out, thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2021년 2월 13일
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

Thank you, that works too and directly uses the table command.
Thanks.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 2월 13일

댓글:

2021년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by