How can I replace an entire row in a table?

12 visualizaciones (últimos 30 días)
MathWorks Support Team
MathWorks Support Team el 12 de Mzo. de 2014
Comentada: Jim Hokanson el 2 de Abr. de 2022
I have a table with many columns. Some of the columns have string elements, while other columns have double values. The code below creates this table:
% Create table columns
col1 = {'one';'two';'three'};
col2 = [1;2;3];
col3 = {'four';'five';'six'};
col4 = [4;5;6];
% Create table
myTable = table(col1,col2,col3,col4);
I also have a cell that has a single row that matches the pattern in the table as follows:
% Create cell
myCell = {'seven',8,'nine',10};
I would like to assign the cell array to replace the first row in the table, but when I execute the following code:
myTable(1,:) = myCell;
I get the following error:
Error using table/subsasgnParens (line 205)
Subscripted assignment dimension mismatch for table variable 'col1'.
Error in table/subsasgn (line 60)
t = subsasgnParens(t,s,b,creating);
How can I replace an entire row within a table?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 30 de Mayo de 2018
The error is expected given that an entire row within a table is itself a table, as such trying to assign a cell array to it is incompatible. In order to replace the entire row with the contents of the cell array you would need to first convert the cell array to a table and then do the assignment as follows:
% Convert cell to 'table'
myTableCell = cell2table(myCell);
% Assign to first row
myTable(1,:) = myTableCell;
  1 comentario
Jim Hokanson
Jim Hokanson el 2 de Abr. de 2022
Doesn't always work ...:
s = struct('a',{'a','b'},'b',{'c','d'})
t1 = struct2table(s);
t2 = struct2table(s(1));
t1(2,:) = t2
Conversion to cell from char is not possible.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos


Versión

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by