How do I extract elements from a table according to specific conditions?

20 visualizaciones (últimos 30 días)
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 :)

Respuesta aceptada

Jeff Miller
Jeff Miller el 25 de Mayo de 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
  1 comentario
Kyle Davis
Kyle Davis el 25 de Mayo de 2019
Thank you so much, your solution using categorical variables worked!! Really appreciate your help :)

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 24 de Mayo de 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 comentarios
Kyle Davis
Kyle Davis el 24 de Mayo de 2019
Thank you for your response.
I have tried to use your suggestion, however Matlab produces the following error:
"Undefined operator '==' for input arguments of type 'cell' ".
T1(:, T1.TargetStatus == 'T' & T1.Response == 'k');
Undefined operator '==' for input arguments of type
'cell'.
Sorry, I didn't realise that the data was type 'cell'.
In this case, would I have to use the 'strcmp' command as you previously mentioned? If so how would I implement that into the code? I've played around with it but can't seem to get it to do what I want it to!
Thanks :)
Guillaume
Guillaume el 24 de Mayo de 2019
Yes, you'd have to use strcmp. It's more or less the same:
T1(:, strcmp(T1.TargetStatus, 'T') & strcmp(T1.Response, 'k'))

Iniciar sesión para comentar.

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by