Borrar filtros
Borrar filtros

How do you clear the contents of a table?

105 visualizaciones (últimos 30 días)
Sonoma Rich
Sonoma Rich el 3 de Mzo. de 2016
Comentada: Walter Roberson el 26 de Mayo de 2022
I have a table and I want to clear the contents of the table, but not delete it. For example, lets say I have a table T: T = table(1,2,{'a'}) I tried to clear the contents by entering: T(1,:)=[]; This deleted the row. I wanted to set the contents to "[]", not delete the row. Is there a way to do this?

Respuestas (3)

Chris
Chris el 11 de Nov. de 2020
Editada: Chris el 11 de Nov. de 2020
I know this is old, but figured it might help someone else. For a more generic approach: Create a table with 1 row of empty values with the data types matching your original table . Assign that 'empty' table to the row you wish to clear in your original table. For example:
%Creating the example table (original table)
d = [1:3]';% type double
t = [datetime;datetime;datetime]; %type datetime
s = ["hello";"yellow";"sandpaper"]; %type string
T = table(d,t,s); %create table
%Determine variable types for each column of original table
varTypes = varfun(@class,T,'OutputFormat','cell');
%Creating the 'empty' table with 1 row
%(matching the correct variable types with the orginal table (T))
T2 = table('Size',[1,width(T)],'VariableTypes',varTypes);
%Clearing row 2 of the original example table (T)
row2Clear = 2;% The row that will be cleared
T(row2Clear,:) = T2;%clearing the row but preserving the overall table
disp(T)
This was the cleanest way I could figure out how to do it.
*Note for doubles MATLAB sets the field in the table to 0 when you clear it (unless I am missing something).

Walter Roberson
Walter Roberson el 11 de Nov. de 2020
Provided that your table does not contain cell (or possibly some other datatypes),
for K = 1 : width(T); T.(K) = missing; end
For variables that are cell, you should assign cell(height(T),1)
  1 comentario
Walter Roberson
Walter Roberson el 26 de Mayo de 2022
Note that if you have numeric entries such as 2, and you want to replace the rows with [] rather than nan, then it would be necessary to change the type of the variable from double to cell. A numeric column cannot contain []

Iniciar sesión para comentar.


MHN
MHN el 4 de Mzo. de 2016
Editada: MHN el 4 de Mzo. de 2016
It depends on the structure of your columns. The easiest way is to put NaN on the raw (if there is a numerical column), then you can find your complete rows with ismissing.
If you insist on clearing the contents then (actually cleaning depends on the type of your data in each column) :
T = table([1; 2;3],[7;8;9],[{'ab'}; {'b'}; {'cd'}])
T{1,1:2}=NaN; T{1,3}={''};
  3 comentarios
Sonoma Rich
Sonoma Rich el 8 de Mzo. de 2016
The problem with this method is that it requires you to know the type of data in each table cell. Lets say you did not know the type (numeric or non-numeric). Is there a simple way to clear the contents? I could write a for loop with a test, but it is not desirable.
MHN
MHN el 8 de Mzo. de 2016
Editada: MHN el 8 de Mzo. de 2016
You can find the data type by just one loop on the first row.
Another solution (if you are not insisting in showing empty rows) is to add one column to your table (for example ID = 1:height(table)) and then change the ID of the rows that you would like to clear to NaN. Then just have another table without the rows that start with NaN (you can use ismissing for this purpose). But if you are insisting in clearing the content I believe you should know the data type, otherwise clearing is meaningless since you don't know whether you should put empty string or empty number or ... there.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by