Deduces trial numbers associated with repeated forms in a response table structure. Returns trial numbers (starting at 1 for each subject indicated in the subject_id field) in a new field called trial_number. out_st = ensemble_deduce_trial_number(resp_st,params); This function is useful for generating a variable that can be used to group multiple rows that belong together conceptually. Having such information is necessary for rearranging functions that convert between long and wide formats, such as when different question_id or compqid values are broken out into their own variables. For example, export_respnstim accomplishes this rearrangement by virtue of a consistent stimulus_id across a set of rows. However, non-stimulus related blocks of questions have no grouping information, and so trial_number fulfills that purpose.
0001 function out_st = ensemble_deduce_trial_number(resp_st,params) 0002 % Deduces trial numbers associated with repeated forms in a response table 0003 % structure. Returns trial numbers (starting at 1 for each subject 0004 % indicated in the subject_id field) in a new field called trial_number. 0005 % 0006 % out_st = ensemble_deduce_trial_number(resp_st,params); 0007 % 0008 % This function is useful for generating a variable that can be used to 0009 % group multiple rows that belong together conceptually. Having such 0010 % information is necessary for rearranging functions that convert between 0011 % long and wide formats, such as when different question_id or compqid 0012 % values are broken out into their own variables. For example, 0013 % export_respnstim accomplishes this rearrangement by virtue of a 0014 % consistent stimulus_id across a set of rows. However, non-stimulus 0015 % related blocks of questions have no grouping information, and so 0016 % trial_number fulfills that purpose. 0017 % 0018 % 0019 0020 % 03Sep2014 Petr Janata 0021 % 24Jan2014 PJ - added handling of successive numbering across multiple 0022 % sessions within a single subject 0023 0024 % Make sure we have the columns we need 0025 requiredVars = {'subject_id','response_order','form_order','session_id'}; 0026 if ~all(ismember(requiredVars, resp_st.vars)) 0027 error('Required variables (%s) not in data struct', cell2str(requiredVars,',')) 0028 end 0029 0030 % Add the new column 0031 resp_st.vars{end+1} = 'trial_number'; 0032 resp_st.data{end+1} = zeros(size(resp_st.data{1},1),1); 0033 0034 % Get column names and indices 0035 rcols = set_var_col_const(resp_st.vars); 0036 0037 % Get the number of subjects 0038 subids = unique(resp_st.data{rcols.subject_id}); 0039 nsubs = length(subids); 0040 0041 % Generate a matrix of response and form orders 0042 rofo = [resp_st.data{rcols.response_order} resp_st.data{rcols.form_order}]; 0043 0044 for isub = 1:nsubs 0045 currSub = subids{isub}; 0046 submask = strcmp(resp_st.data{rcols.subject_id}, currSub); 0047 0048 newro = [1; diff(rofo(submask,1))>0]; 0049 newsamefo = [1; diff(rofo(submask,2))<1]; 0050 newsess = [1; diff(resp_st.data{rcols.session_id}(submask))~=0]; 0051 0052 rofomask = newro & newsamefo | newsess; 0053 0054 resp_st.data{rcols.trial_number}(submask) = cumsum(rofomask); 0055 0056 end % for isub 0057 0058 out_st = resp_st; % Assign the output 0059 0060 return