How do you clear the contents of a table?
105 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Antworten (3)
Chris
am 11 Nov. 2020
Bearbeitet: Chris
am 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).
0 Kommentare
Walter Roberson
am 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 Kommentar
Walter Roberson
am 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 []
MHN
am 4 Mär. 2016
Bearbeitet: MHN
am 4 Mär. 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 Kommentare
MHN
am 8 Mär. 2016
Bearbeitet: MHN
am 8 Mär. 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.
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!