Declare a table where each column variable is an array of numbers
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Martin Panevsky
el 13 de Feb. de 2021
Comentada: Martin Panevsky
el 14 de Feb. de 2021
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.
0 comentarios
Respuesta aceptada
Adam Danz
el 13 de Feb. de 2021
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{1,1} = [1,2,3]
4 comentarios
Más respuestas (1)
dpb
el 13 de Feb. de 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}
>>
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!