Sos
Sos

Reputation: 1949

Searching a table by a string

I am searching a table by a string, but I am not finding the result even though I'm 100% sure it is there. Running genelist prints the table:

>> Untitled

genelist = 

    Prot_Family     Gene  
    ___________    _______

    'Prx'          'PRDX1'
    'Prx'          'PRDX2'
    'Prx'          'PRDX3'
    'Trx'          'TXN'  
    'Trx'          'TXN2' 

But running this command does not find the string: find(strcmp('PRDX2',genelist(:,2)))

and strcmp('PRDX2',genelist(:,2)) returns 0.

What am I doing wrong?

Upvotes: 4

Views: 4063

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

The reason this is happening is because when you index a table, even if everything on those index ranges are the same type, MATLAB will return just another table. Including genelist(1,1) will return just a table with a single value on it.

This is because tables are more than just the information they contain in each of the locations. They also have column/row names and a bunch of other information on how to handle them.

Thus, if you want to compare strings from a index range that you know is a string, you need to convert it to string. Use table2array as:

find(strcmp('PRDX2',table2array(genelist(:,2))))

Upvotes: 4

Related Questions