Clear Filters
Clear Filters

Compare content of a cell that has text

1 view (last 30 days)
Hi everyone,
I am trying to extract some time values from a dataset. If a certain condition is satisfied, I want to obtain both the start time and the RT of each row, if not, I want to get a 0. I transformed the initial array into a table, and started to do comparisons there. In the code I am attaching (I hope is sufficiently documented), I compare the response (column5) with the location of a particular condition (e.g. gmax in column 2). If it matches, it returns the values from columns 6,7 (starttime and rt). I want to do this with column 1 (type) which contains three possible values ('rand','trad_zero','trad_neg'). If it matches 'rand' I want to get the start_time and rt from those rows(column 6&7), and save them as random_onsets & random_durations, respectively. Do you have any idea how can I do this? I am converting the array to a table, because is the only way I know how to make the other comparisons between columns (I am pretty new to matlab or any programming language). If you have a suggestion from the scratch, I would appreciate it too. Here is the code, I attached also a sample of the original data:
%%Load the behavioral data and tables for comparison
load ('Gamble_ET_1_S3_block1.mat', 'stim_choice')
%%Create a table with the values we wish to extract and name the variables accordingly
T = table({stim_choice.type}.', [stim_choice.gmax].', [stim_choice.pmax].', [stim_choice.lmin].', [stim_choice.resp_num].', [stim_choice.start_time].', [stim_choice.rt].', [stim_choice.end_time].', 'VariableNames', {'type', 'gmax', 'pmax', 'lmin', 'resp_num', 'start_time', 'rt', 'end_time'});
%%Compare location of variables vs response(column5) to obtain onsets (column6) and durations (column7)
A = (T{:,2} == T{:,[5 5]}).*T{:,6:7}; %gmax
B= (T{:,3} == T{:,[5 5]}).*T{:,6:7}; %pmax
C = (T{:,4} == T{:,[5 5]}).*T{:,6:7}; %lmin
%%Obtain onsets and durations of each condition
%Simple scenarios
decision_onsets = (T{:,6});
decision_durations = (T{:,7});
postdecision_onsets = (T{:,8}); % = End time
postdecision_durations = 12-(T{:,7}); % = Trial duration (12s) - rt
%Complex scenarios
%---------Separate onsets and durations & remove 0's--------
gmax_onsets = nonzeros (A(:,1));
gmax_durations = nonzeros (A(:,2));
pmax_onsets = nonzeros (B(:,1));
pmax_durations = nonzeros (B(:,2));
lmin_onsets = nonzeros (C(:,1));
lmin_durations = nonzeros (C(:,2));
Many thanks, Ramiro
  2 Comments
KL
KL on 10 Jul 2017
Your conditions are quite not clear. It would be easier if you tell us what do you expect in the resultant array/table.
Ramiro Rea
Ramiro Rea on 10 Jul 2017
Sorry for that, I want an array (without the 0) of the timestamps of every condition. For every condition I will have two arrays, "onsets" and "durations", which are start_time and rt of every trial. I hope this clarifies my question. Anyway, your solution worked perfectly.

Sign in to comment.

Accepted Answer

KL
KL on 10 Jul 2017
if you want to extract start_time and rt based on some conditions, you could create a boolean array.
gC = T.resp_num==T.gmax;
pC = T.resp_num==T.pmax;
tC = strcmp('rand',T.type);
pCT = [T.start_time(pC) T.rt(pC)]
gCT = [T.start_time(gC) T.rt(gC)]
tCT = [T.start_time(tC) T.rt(tC)]
| strcmp | is a command used to compare two strings, just like the == operator for numbers.
  1 Comment
Ramiro Rea
Ramiro Rea on 10 Jul 2017
Thank you very much. This worked perfectly. I'll remember that about strcmp.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!