How do you clear the contents of a table?

109 vues (au cours des 30 derniers jours)
Sonoma Rich
Sonoma Rich le 3 Mar 2016
Commenté : Walter Roberson le 26 Mai 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?

Réponses (3)

Chris
Chris le 11 Nov 2020
Modifié(e) : Chris le 11 Nov 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 le 11 Nov 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 commentaire
Walter Roberson
Walter Roberson le 26 Mai 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 []

Connectez-vous pour commenter.


MHN
MHN le 4 Mar 2016
Modifié(e) : MHN le 4 Mar 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 commentaires
Sonoma Rich
Sonoma Rich le 8 Mar 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 le 8 Mar 2016
Modifié(e) : MHN le 8 Mar 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by