mHelpMe
mHelpMe

Reputation: 6668

Searching cell for string and return index number

I have a 20 x 2 cell. Both columns contains strings. What I would like to do is search for a string in column two and where the value in column 2 matches my search string to return the value in column 1. Please see my example below - I believe it will make things clearer.

name         region          
ABC          USA
ASD          EU
PLKDD        EU
ERT          EU
LKK          ASIA
MNN          USA
WER          EU

The result I would like based on the search string being "EU" is below

result
ASD
PLKDD
ERT
WER

Upvotes: 0

Views: 54

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 14939

You can try ismember:

x = 
    'ABC'      'USA' 
    'ASD'      'EU'  
    'PLKDD'    'EU'  
    'ERT'      'EU'  
    'LKK'      'ASIA'
    'MNN'      'USA' 
    'WER'      'EU'  

y = x(find(ismember(x(:,2), 'EU')),1)   
y = 
    'ASD'
    'PLKDD'
    'ERT'
    'WER'

Upvotes: 1

Related Questions