How do I extract elements from a table according to specific conditions?
Show older comments
Hi,
I am trying to extract certain rows of data from the following table (T1) according to specfic conditions. I only want to extract a row if 'TargetStatus' is equal to 'T' and t the same time, 'Response' is equal to 'k' (such as row 2 and row 7 for example). I'm struggling to come up with the correct code that will allow me to do this. I have copied the table below :)
T1 =
24×4 table
Sequence TargetStatus ReactionTime Response
________ ____________ ____________ ________
'C' 'N' 1.1022 'j'
'C' 'T' 1.4261 'k'
'T' 'N' 0.77136 'j'
'D' 'N' 0.77733 'j'
'K' 'N' 1.044 'j'
'X' 'N' 1.0333 'j'
'X' 'T' 0.82694 'k'
'B' 'N' 0.89246 'j'
'W' 'N' 0.86045 'j'
'H' 'N' 0.73916 'j'
'H' 'T' 0.84648 'k'
'T' 'N' 0.91134 'j'
'T' 'T' 0.91947 'k'
'G' 'N' 0.70199 'j'
'G' 'T' 0.81821 'k'
'Z' 'N' 0.79684 'j'
'Q' 'N' 0.94614 'j'
'J' 'N' 0.83011 'j'
'Z' 'N' 0.85475 'j'
'F' 'N' 0.72073 'j'
'F' 'T' 0.64145 'k'
'B' 'N' 0.66913 'j'
'X' 'N' 0.71484 'j'
'X' 'T' 0.8122 'k'
Thank you to anyone who is able to help :)
Accepted Answer
More Answers (1)
Guillaume
on 24 May 2019
T1(:, T1.TargetStatus == 'N' & T1.Response == 'k') %select all rows for which TargetStatus is 'N' AND Response is 'k'
Note that == works here because your char arrays are just one character. If they had more than one character, you'd use strcmp instead.
2 Comments
Kyle Davis
on 24 May 2019
Guillaume
on 24 May 2019
Yes, you'd have to use strcmp. It's more or less the same:
T1(:, strcmp(T1.TargetStatus, 'T') & strcmp(T1.Response, 'k'))
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!