tables remove one field

1 visualización (últimos 30 días)
Patrick Brown
Patrick Brown el 23 de Feb. de 2017
Respondida: Peter Perkins el 28 de Feb. de 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

Respuestas (3)

Chibuzo Nnonyelu
Chibuzo Nnonyelu el 23 de Feb. de 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 comentario
Jan
Jan el 23 de Feb. de 2017
Editada: Jan el 23 de Feb. de 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}))

Iniciar sesión para comentar.


Patrick Brown
Patrick Brown el 23 de Feb. de 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 el 28 de Feb. de 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".

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by