Can I use 'table' as a variable type in a table
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Stephen23
il 26 Feb 2025
Modificato: Stephen23
il 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).
Peter Perkins
il 3 Mar 2025
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}
Risposta accettata
Voss
il 26 Feb 2025
InnerTable = table('Size',[1 2],'VariableTypes',{'double', 'double'});
InnerTable{2,1} = 14.34;
InnerTable{3,2} = 45
Table = table('Size',[height(InnerTable) 3],'VariableTypes',{'table', 'double', 'struct'})
% 4 alternatives:
Table.(1) = InnerTable;
Table.Var1 = InnerTable;
Table{:,1} = InnerTable;
Table{:,'Var1'} = InnerTable;
Table
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Tables in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!