How to remove rows that do not contain or match a specific 'string'

42 visualizaciones (últimos 30 días)
Sean Byrne
Sean Byrne el 18 de Abr. de 2018
Editada: Stephen23 el 19 de Abr. de 2018
What I am trying to do is remove rows in my cell array "Vars" that do not contain any of the values in the cell array "Names"
For example I have a an array of data ive created/imported
Vars=
'Subject' 'Mass' 'Height' 'Age'
'A' 90 170 20
'B' 100 182 20
'C' 85 180 21
'D' 80 185 21
'E' 79 175 20
But I only want a final array containing the values corresponding with the strings in the array "names"
Names=
'Subject'
'A'
'C'
'D'
So the final product should look like
FinalVars =
'Subject' 'Mass' 'Height' 'Age'
'A' 90 170 20
'C' 85 180 21
'D' 80 185 21
Thankyou in advance for your help,
Note** the string 'A', 'B' etc will most likely be a last name ie. 'smith' or 'john' etc.
  1 comentario
Stephen23
Stephen23 el 19 de Abr. de 2018
Editada: Stephen23 el 19 de Abr. de 2018
Storing numeric scalars in a cell array makes processing that data much more difficult. Some beginners like the idea of storing any headers and footers together with their data in a cell array, but it is really much simpler to keep numeric data stored in a numeric array, and the headers and footers in their own cell vectors.
Alternatively you could use a table, which is exactly designed for data arranged like yours:

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 19 de Abr. de 2018
Editada: Stephen23 el 19 de Abr. de 2018

For a cell array you can use ismember:

Vars = {
'Subject' 'Mass' 'Height' 'Age'
'A' 90 170 20
'B' 100 182 20
'C' 85 180 21
'D' 80 185 21
'E' 79 175 20};
Names = {'Subject','A','C','D'};
idx = ismember(Vars(:,1),Names);
out = Vars(idx,:)

Categorías

Más información sobre Cell Arrays 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