Can I use 'table' as a variable type in a table

조회 수: 3 (최근 30일)
Ronald Stahl
Ronald Stahl 2025년 2월 26일
댓글: Peter Perkins 2025년 3월 3일
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;
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing default values.
InnerTable{3,2} = 45;
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing default values.
Table = table('Size',[1 3],'VariableTypes',{'table', 'double', 'struct'});
Table{1,1} = InnerTable;
Error using {} (line 161)
The value on the right-hand side of the assignment has the wrong width. The assignment requires a value whose width is 0.
Can a table be assigned to a location within a table? How?
  댓글 수: 5
Stephen23
Stephen23 2025년 2월 26일
편집: Stephen23 2025년 2월 26일
"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).
Peter Perkins
Peter Perkins 2025년 3월 3일
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)
t = 3x3 table
t1 t2 x ________ ________ __ V1 V2 V3 V4 __ __ __ __ 1 4 7 10 13 2 5 8 11 14 3 6 9 12 15
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)
t = 3x2 table
Var1 x ___________ __ {3x2 table} 15 {4x2 table} 16 {0x2 table} 17
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
ans = 3x1 cell array
{3x2 table} {4x2 table} {0x2 table}
and t.Var1{1} is t1
t.Var1{1}
ans = 3x2 table
V1 V2 __ __ 1 4 2 5 3 6

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

채택된 답변

Voss
Voss 2025년 2월 26일
InnerTable = table('Size',[1 2],'VariableTypes',{'double', 'double'});
InnerTable{2,1} = 14.34;
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing default values.
InnerTable{3,2} = 45
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing default values.
InnerTable = 3x2 table
Var1 Var2 _____ ____ 0 0 14.34 0 0 45
Table = table('Size',[height(InnerTable) 3],'VariableTypes',{'table', 'double', 'struct'})
Table = 3x3 table
Var1 Var2 Var3 _________ ____ __________ 1x0 table 0 1x1 struct 1x0 table 0 1x1 struct 1x0 table 0 1x1 struct
% 4 alternatives:
Table.(1) = InnerTable;
Table.Var1 = InnerTable;
Table{:,1} = InnerTable;
Table{:,'Var1'} = InnerTable;
Table
Table = 3x3 table
Var1 Var2 Var3 _____________ ____ __________ Var1 Var2 _____ ____ 0 0 0 1x1 struct 14.34 0 0 1x1 struct 0 45 0 1x1 struct
  댓글 수: 2
Ronald Stahl
Ronald Stahl 2025년 2월 26일
Thank you for the detailed explanation. The limits on using a table within a table make this the wrong approach for solving my coding problem.
Voss
Voss 2025년 2월 26일
편집: Voss 2025년 2월 26일
You're welcome!
Consider using a cell array for any table variable that needs to be able to contain anything, as Stephen23 suggested above.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by