How do I extract elements from a table according to specific conditions?
20 views (last 30 days)
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 :)
0 Comments
Accepted Answer
Jeff Miller
on 25 May 2019
I think if you want to select rows then the colon operator is in the wrong place, and that you would want something like
T1(strcmp(T1.TargetStatus, 'T') & strcmp(T1.Response, 'k'),:);
But it might really be more convenient to use categorical variables, like this
T1.Sequence = categorical(T1.Sequence)
T1.TargetStatus = categorical(T1.TargetStatus)
T1.Response = categorical(T1.Response)
T1(T1.TargetStatus=='N' & T1.Response=='k',:)
Since you are analyzing RT data, you might find the RawRT toolbox useful. It won't work with categorical variables, so you would have to convert everything to numbers, like this:
T1.Sequence = grp2idx(T1.Sequence);
T1.TargetStatus = grp2idx(T1.TargetStatus);
T1.Response = grp2idx(T1.Response);
But then you could compute a lot of summary stats, graphs, ANOVAs, etc pretty simply, e.g.
somemeans = CondMeans(T1,'ReactionTime',{'TargetStatus','Response'}) % which produces
somemeans =
TargetStatus Response ReactionTime
____________ ________ ____________
1 1 0.84507
2 2 0.89869
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
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'))
See Also
Categories
Find more on Numeric Types 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!