dynamic
dynamic

Reputation: 48111

Filter cell array by some string value

I have a cell array, something like this:

A = 

    '5523'    '2012-10-26'    '23'    'T'    '17.7'
    '5513'    '2012-10-26'    '23'    'T'    '22.1'
    '5506'    '2012-10-26'    '23'    'C'    '16.2'

Now I would like to filter all records that have T. So I would like to get this array:

A = 

    '5523'    '2012-10-26'    '23'    'T'    '17.7'
    '5513'    '2012-10-26'    '23'    'T'    '22.1'

I could parse all array, but is there any other way?

Upvotes: 2

Views: 7552

Answers (1)

shoelzer
shoelzer

Reputation: 10708

Here's a one-liner to do it:

A = A(strcmp(A(:,4), 'T'), :);

The inner part, strcmp(A(:,4), 'T'), is comparing column 4 of all rows to 'T'. Then that boolean vector can extract matching rows from A with logical indexing.

Upvotes: 6

Related Questions