How do you clear the contents of a table?

조회 수: 104 (최근 30일)
Sonoma Rich
Sonoma Rich 2016년 3월 3일
댓글: Walter Roberson 2022년 5월 26일
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?

답변 (3개)

Chris
Chris 2020년 11월 11일
편집: Chris 2020년 11월 11일
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 2020년 11월 11일
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
Walter Roberson
Walter Roberson 2022년 5월 26일
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
MHN 2016년 3월 4일
편집: MHN 2016년 3월 4일
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
Sonoma Rich
Sonoma Rich 2016년 3월 8일
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 2016년 3월 8일
편집: MHN 2016년 3월 8일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by