tables remove one field

1 vue (au cours des 30 derniers jours)
Patrick Brown
Patrick Brown le 23 Fév 2017
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

Réponses (3)

Chibuzo Nnonyelu
Chibuzo Nnonyelu le 23 Fév 2017
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 commentaire
Jan
Jan le 23 Fév 2017
Modifié(e) : Jan le 23 Fév 2017
Or:
if all(strcmp('No', table2cell(T(:, m))))
I cannot try this currently, but would this work also:
if all(strcmp('No', T{:, m}))

Connectez-vous pour commenter.


Patrick Brown
Patrick Brown le 23 Fév 2017
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 le 28 Fév 2017
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".

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