MATLAB: Compare content of a cell that has text

compare cellscompare columnsMATLAB

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

Best Answer

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.