How do you clear the contents of a table?

101 ビュー (過去 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 Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by