Effacer les filtres
Effacer les filtres

Detecting a particular word in a cell vector

2 vues (au cours des 30 derniers jours)
Sabbas
Sabbas le 9 Juil 2012
Dear all,
I have a matrix whose first column is :
'MAR'
' POPSIZE'
'OTHTOTAL'
' CONIZE'
' POPIZE'
'MAR'
' POPULAR SIZE'
'OTHOTAL'
' CONSIIZE'
' POPUZE'
'MAR'
'PRODGORY'
I want to detect if the above column contains the string element ‘MAR’
thanks

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 9 Juil 2012
Modifié(e) : Andrei Bobrov le 9 Juil 2012
eg
A = {'MAR'
' POPSIZE'
'OTHTOTAL'
' CONIZE'
' POPIZE'
'MAR'
' POPULAR SIZE'
'OTHOTAL'
' CONSIIZE'
' POPUZE'
'MAR'
'PRODGORY'}
out = any(ismember(A,'MAR'));
or
out = any(strcmp(A,'MAR'));
  1 commentaire
Jan
Jan le 9 Juil 2012
STRCMP is faster.

Connectez-vous pour commenter.

Plus de réponses (2)

Luffy
Luffy le 9 Juil 2012
Modifié(e) : Luffy le 9 Juil 2012
see if any(strcmp(M,'MAR')) works for you, if there is MAR it shows 1 else 0
Example: If M = {'MAR'
'POPSIZE'
'OTHTOTAL'
' CONIZE'
' POPIZE'
'MAR'
' POPULAR SIZE'
'OTHOTAL'
' CONSIIZE'
' POPUZE'
'MAR'
'PRODGORY'}
use strcmp(M,'MAR')

Albert Yam
Albert Yam le 9 Juil 2012
I would use a regexp. http://www.mathworks.com/help/techdoc/ref/regexp.html Perhaps with a 'match' option, depending what you want as an output.
test = { 'MAR'
' POPSIZE'
'OTHTOTAL'
' CONIZE'
' POPIZE'
'MAR'
' POPULAR SIZE'
'OTHOTAL'
' CONSIIZE'
' POPUZE'
'MAR'
'PRODGORY'};
regexp(test,'MAR')
  1 commentaire
Sabbas
Sabbas le 9 Juil 2012
all of you three were correct and I thank you

Connectez-vous pour commenter.

Catégories

En savoir plus sur Cell Arrays 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