tables remove one field

1 回表示 (過去 30 日間)
Patrick Brown
Patrick Brown 2017 年 2 月 23 日
回答済み: Peter Perkins 2017 年 2 月 28 日
I have three fields in a table
T.Position= 'No' '10' '20'
T.Velocity= 'No' '10' '60'
T.Acceleration= 'No' 'No' 'No'
then I want to eliminate the field that have all values 'No' which is only Acceleration
How I can do it??
Thanks

回答 (3 件)

Chibuzo Nnonyelu
Chibuzo Nnonyelu 2017 年 2 月 23 日
Goes through each column and deletes a column if the number of 'No' found in the column is equal to the height of the table.
for m = 1:width(T)
if length(strfind(strjoin(table2cell(T(:, m))), 'No') ) == height(T)
% delete the column;
T(:, m) = [];
end
end
  1 件のコメント
Jan
Jan 2017 年 2 月 23 日
編集済み: Jan 2017 年 2 月 23 日
Or:
if all(strcmp('No', table2cell(T(:, m))))
I cannot try this currently, but would this work also:
if all(strcmp('No', T{:, m}))

サインインしてコメントする。


Patrick Brown
Patrick Brown 2017 年 2 月 23 日
It does not work well because you have this error Variable index exceeds table dimensions.
maybe because you are changing the width of T when you delete columns

Peter Perkins
Peter Perkins 2017 年 2 月 28 日
I'll assume that your table looks like this:
>> t = table({'No'; 10; 20}, {'No'; 10; 60}, {'No'; 'No'; 'No'}, 'VariableNames',{'Position' 'Velocity' 'Acceleration'})
t =
Position Velocity Acceleration
________ ________ ____________
'No' 'No' 'No'
[10] [10] 'No'
[20] [60] 'No'
(which is not at all clear). How would you do this with any MATLAB array? The answer is essentially the same: loop over the variables in the table, deleting the ones that are all No's, and loop backwards so you're not deleting things out from under yourself. "dot-parens-index" dubscripting makes this almost identical to what you'd do for a double array.
for j = width(t):-1:1
if all(strcmp(t.(j),'No'))
t.(j) = [];
end
end
Another way is to use varfun to figure out what to delete, and then delete all at once.
toDeleteFun = @(x) all(strcmp(x,'No'));
toDelete = varfun(toDeleteFun,t,'OutputFormat','uniform')
t(:,toDelete) = [];
The terminology for tables is "variables", not "fields" or "columns".

カテゴリ

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