Home > matlabmk > cmpt_corrMK.m

cmpt_corrMK

PURPOSE ^

compile_erpimageMK() - Extracts relevant data from multiple data sets for

SYNOPSIS ^

function cmpt_corrMK(setfiles,out_fname,varargin)

DESCRIPTION ^

 compile_erpimageMK() - Extracts relevant data from multiple data sets for
                        creating ERPimages.  ERPimages are produced by the
                        function plot_erpimageMK.m.
              
 Usage:
  >> compile_erpimageMK(setfiles,varargin)

 Required Inputs:
   setfiles     = [string | cell array | integer vector] Identifies the 
                   EEGLAB .set files to be used for constructing ERPimages.
                   Can be the name of a single set file (a string), a cell
                   array of filenames (each element of the array is a 
                   string) or a vector of integers specifying the ID 
                   numbers of the participants to be used.  If setfiles is 
                   an integer vector, the optional argument "setfile_tplate" 
                   (see below) must be used. If setfiles is a string or 
                   cell array, include the file's path unless the file is 
                   the current working directory.
     

 Optional Inputs:
   'bins'           = [integer vector] Bins to extract trials from {default:
                       use all bins}
   'chans'          = [string | cell array] Name of the electrode (e.g., 
                       'MiCe') or a cell array of electrode names that you 
                       want to image. {default: use all electrodes}
   'setfile_tplate' = [string] set filename template (i.e., a string with
                       # where the subject number should be--see example 
                       below).  The argument "setfiles" must be a vector 
                       of participant numbers to use this option.  Include
                       the files' path unless the files are in the current
                       working directory. {default: none}
   'out_fname'      = [string] The name of the Matlab file in which the
                       ERPimage data will be stored.  If not specified, 
                       the data will be stored in global variables that 
                       plot_erpimageMK.m can use.  By convention, you 
                       should give the file the extension .eim and include 
                       the files' path, unless the files are in the 
                       current working directory. {default: store data as
                       global variables}
   'verblevel'      = an integer specifiying the amount of information you 
                       want functions to provide about what they are doing 
                       during runtime.
                        Options are:
                         0 - quiet, only show errors, warnings, and EEGLAB 
                             reports
                         1 - stuff anyone should probably know
                         2 - stuff you should know the first time you start 
                             working with a data set {default value if 
                             verblevel not already specified}
                         3 - stuff that might help you debug (show all 
                             reports)

 Outputs:
   If specified, function saves data for creating ERPimages in a Matlab file.
   Otherwise, function stores the data in the following global variables:
    chan_data, chans, chanlocs, ep_info, ep_times, bindesc, & srate


 Example: 
 >> setfile_tplate='/homes/dgroppe/SANDBOX/RTERPM/rterpm#.set';
 >> chans={'MiPf','MiCe','MiOc','LLOc','RLOc'};
 >> compile_erpimageMK(1:4,'setfile_tplate',setfile_tplate,'chans',chans, ...
    'bins',[127 133 142 157],'out_fname','demo.eim');
 >> plot_erpimageMK('MiCe','rtmsec','in_fname','demo.eim','avewidth',4,'fig_id',1);

 Notes: 
 -This function assumes that if two electrodes in two different setfiles
 have the same name (e.g., 'MiCe'), then they are homologous.  Congruency
 of electrode coordinates is not checked.  Some electrodes in the 26 and
 59 electrode caps have the same names but are not in homologous
 positions.

 Author: 
 David Groppe
 Kutaslab, 8/2009

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function cmpt_corrMK(setfiles,out_fname,varargin)
0002 % compile_erpimageMK() - Extracts relevant data from multiple data sets for
0003 %                        creating ERPimages.  ERPimages are produced by the
0004 %                        function plot_erpimageMK.m.
0005 %
0006 % Usage:
0007 %  >> compile_erpimageMK(setfiles,varargin)
0008 %
0009 % Required Inputs:
0010 %   setfiles     = [string | cell array | integer vector] Identifies the
0011 %                   EEGLAB .set files to be used for constructing ERPimages.
0012 %                   Can be the name of a single set file (a string), a cell
0013 %                   array of filenames (each element of the array is a
0014 %                   string) or a vector of integers specifying the ID
0015 %                   numbers of the participants to be used.  If setfiles is
0016 %                   an integer vector, the optional argument "setfile_tplate"
0017 %                   (see below) must be used. If setfiles is a string or
0018 %                   cell array, include the file's path unless the file is
0019 %                   the current working directory.
0020 %
0021 %
0022 % Optional Inputs:
0023 %   'bins'           = [integer vector] Bins to extract trials from {default:
0024 %                       use all bins}
0025 %   'chans'          = [string | cell array] Name of the electrode (e.g.,
0026 %                       'MiCe') or a cell array of electrode names that you
0027 %                       want to image. {default: use all electrodes}
0028 %   'setfile_tplate' = [string] set filename template (i.e., a string with
0029 %                       # where the subject number should be--see example
0030 %                       below).  The argument "setfiles" must be a vector
0031 %                       of participant numbers to use this option.  Include
0032 %                       the files' path unless the files are in the current
0033 %                       working directory. {default: none}
0034 %   'out_fname'      = [string] The name of the Matlab file in which the
0035 %                       ERPimage data will be stored.  If not specified,
0036 %                       the data will be stored in global variables that
0037 %                       plot_erpimageMK.m can use.  By convention, you
0038 %                       should give the file the extension .eim and include
0039 %                       the files' path, unless the files are in the
0040 %                       current working directory. {default: store data as
0041 %                       global variables}
0042 %   'verblevel'      = an integer specifiying the amount of information you
0043 %                       want functions to provide about what they are doing
0044 %                       during runtime.
0045 %                        Options are:
0046 %                         0 - quiet, only show errors, warnings, and EEGLAB
0047 %                             reports
0048 %                         1 - stuff anyone should probably know
0049 %                         2 - stuff you should know the first time you start
0050 %                             working with a data set {default value if
0051 %                             verblevel not already specified}
0052 %                         3 - stuff that might help you debug (show all
0053 %                             reports)
0054 %
0055 % Outputs:
0056 %   If specified, function saves data for creating ERPimages in a Matlab file.
0057 %   Otherwise, function stores the data in the following global variables:
0058 %    chan_data, chans, chanlocs, ep_info, ep_times, bindesc, & srate
0059 %
0060 %
0061 % Example:
0062 % >> setfile_tplate='/homes/dgroppe/SANDBOX/RTERPM/rterpm#.set';
0063 % >> chans={'MiPf','MiCe','MiOc','LLOc','RLOc'};
0064 % >> compile_erpimageMK(1:4,'setfile_tplate',setfile_tplate,'chans',chans, ...
0065 %    'bins',[127 133 142 157],'out_fname','demo.eim');
0066 % >> plot_erpimageMK('MiCe','rtmsec','in_fname','demo.eim','avewidth',4,'fig_id',1);
0067 %
0068 % Notes:
0069 % -This function assumes that if two electrodes in two different setfiles
0070 % have the same name (e.g., 'MiCe'), then they are homologous.  Congruency
0071 % of electrode coordinates is not checked.  Some electrodes in the 26 and
0072 % 59 electrode caps have the same names but are not in homologous
0073 % positions.
0074 %
0075 % Author:
0076 % David Groppe
0077 % Kutaslab, 8/2009
0078 %
0079 
0080 %%%%%%%%%%%%%%%% REVISION LOG %%%%%%%%%%%%%%%%%
0081 %
0082 %
0083 
0084 global VERBLEVEL
0085 % global chan_data;
0086 % global chans;
0087 % global chanlocs;
0088 % global ep_info;
0089 % global ep_times;
0090 % global bindesc;
0091 % global srate;
0092 % global rtbins;
0093 % global rtmsec;
0094 % global eim_bins
0095 
0096 %Input Parser
0097 p=inputParser;
0098 %Note: the order of the required arguments needs to match precisely their
0099 %order in the function definition (which is also the order used by p.parse
0100 %below)
0101 p.addRequired('setfiles',@(x) (ischar(x) || isnumeric(x)) || iscell(x));
0102 p.addRequired('out_fname',@ischar);
0103 p.addParamValue('setfile_tplate',[],@ischar);
0104 p.addParamValue('verblevel',[],@(x) x>=0);
0105 p.addParamValue('cor_vars',[],@(x) (ischar(x) || iscell(x));
0106 p.addParamValue('ignore_vars',[],@(x) (ischar(x) || iscell(x));
0107 %p.addParamValue('bins',[],@isnumeric);
0108 %p.addParamValue('chans',[],@(x) ischar(x) || iscell(x));
0109 
0110 p.parse(setfiles,out_fname,varargin{:});
0111 
0112 %Verblevel
0113 if isempty(VERBLEVEL) && isempty(p.Results.verblevel)
0114     VERBLEVEL=2;
0115 elseif ~isempty(p.Results.verblevel)
0116     VERBLEVEL=p.Results.verblevel;
0117 end
0118 
0119 %Input set files
0120 if ischar(setfiles),
0121    sfiles{1}=setfiles; %make it a cell array
0122 elseif iscell(setfiles),
0123    sfiles=setfiles;
0124 elseif isempty(setfiles),
0125     error('Argument setfiles is currently empty.  You need to specify something.');
0126 else %must be numeric
0127    if isempty(p.Results.setfile_tplate)
0128       error(['If setfiles argument is numeric, you must specify setfile_tplate.' ...
0129           ' Type ">>help mk_erpimage" for an example.']);
0130    else
0131        sfiles=cell(1,length(setfiles));
0132        sfile_ct=0;
0133        pnd_id=find(p.Results.setfile_tplate=='#');
0134        if length(pnd_id)>1 || isempty(pnd_id)
0135           error(['You must use the "#" character in your setfile name template ' ...
0136               'to indicate where the participant numbers should go.']);
0137        end
0138        beg_tplate=p.Results.setfile_tplate(1:pnd_id-1);
0139        end_tplate=p.Results.setfile_tplate(pnd_id+1:end);
0140        for a=setfiles,
0141           sfile_ct=sfile_ct+1;
0142           sfiles{sfile_ct}=[beg_tplate int2str(a) end_tplate];
0143       end
0144    end
0145 end
0146 
0147 %correlation variables to ignore
0148 ignore_vars{1}='event';
0149 ignore_vars{2}='eventlatency';
0150 ignore_vars{3}='eventccode';
0151 ignore_vars{4}='eventevcode';
0152 ignore_vars{5}='eventlogflag';
0153 if ~isempty(p.Results.ignore_vars),
0154     if ischar(p.Results.ignore_vars),
0155         ignore_vars{length(ignore_vars)+1}=p.Results.ignore_vars; %make it a cell array
0156     elseif iscell(p.Results.ignore_vars),
0157         for a=1:length(p.Results.ignore_vars),
0158             ignore_vars{length(ignore_vars)+1}=p.Results.ignore_vars{a};
0159         end
0160     end
0161 end
0162 
0163 %correlation variables to use (if not specified, use all)
0164 if ~isempty(p.Results.cor_vars),
0165     if ischar(p.Results.cor_vars),
0166         cor_vars{1}=p.Results.cor_vars; %make it a cell array
0167     elseif iscell(p.Results.cor_vars),
0168         cor_vars=p.Results.cor_vars;
0169     end
0170 end
0171 
0172 %%%%%%%%%%%%%%%%%%%% Initialize Variables %%%%%%%%%%%%%%%%%%%%%%%%%
0173 clear ep_info; %the epoch information from all sets
0174 rtmsec=[]; %the rts from all sets
0175 rtbins=[]; %the bin numbers that rtmsec correspond to (same event can have different RTs for different bins)
0176 grand_ep_ct=0; 
0177 use_ep_this_set=cell(1,length(sfiles));
0178 
0179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0180 % I. Loop through files once to make sure key parameters are consistent &
0181 %    to get sorting variables
0182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0183 VerbReport('Loading set files to check for consistency and to extract sorting variabls.',2,VERBLEVEL);
0184 for set_ct=1:length(sfiles),
0185     %Note, pop_loadset displays message about what file is being loaded
0186     EEG=pop_loadset(sfiles{set_ct});
0187     
0188     if set_ct==1,
0189         %Keep track of key data parameters to ensre consistency
0190         
0191         %SAMPLING RATE
0192         srate=EEG.srate;
0193         
0194         %TIMES (in ms) FOR ALL TIME POINTS
0195         ep_times=EEG.times;
0196         
0197         %BIN DESCRIPTORS
0198         bindesc=EEG.bindesc;
0199         n_bin=length(bindesc);
0200         
0201         %CHANNEL INFO
0202         chanlocs=EEG.chanlocs;
0203         n_chan=length(chanlocs);
0204         
0205         %REACTION TIME INFO
0206         fldnames=fieldnames(EEG);
0207         rtmsec_attached=0;
0208         rtbins_attached=0;
0209         for d=1:length(fldnames),
0210             if strcmpi(fldnames{d},'rtmsec')
0211                 rtmsec_attached=1;
0212             elseif strcmpi(fldnames{d},'rtbins')
0213                 rtbins_attached=1;
0214             end
0215         end
0216         rts_attached=rtbins_attached*rtmsec_attached;
0217  
0218         
0219         %EPOCH INFORMATION
0220         epinfo_fldnames=fieldnames(EEG.epoch);
0221         use_ep_this_set{set_ct}=[];
0222         %if isempty(p.Results.bins), %grab all bins
0223         for a=1:length(EEG.epoch),
0224             EEG.epoch(a).sourcefile=sfiles{set_ct}; %create new field to keep
0225             %track of what file each epoch came from
0226             for j=1:length(EEG.epoch(a).eventtype), %check this epoch's types
0227                 if strcmpi(EEG.epoch(a).eventtype{j}(1:3),'bin') %is this epoch in a bin?
0228                     use_ep_this_set{set_ct}=[use_ep_this_set{set_ct} a];
0229                     grand_ep_ct=grand_ep_ct+1;
0230                     ep_info(grand_ep_ct)=EEG.epoch(a);
0231                     if rts_attached,
0232                         rtbins{grand_ep_ct}=EEG.rtbins{a};
0233                         rtmsec{grand_ep_ct}=EEG.rtmsec{a};
0234                     end
0235                     break; %leave j loop
0236                 end
0237             end
0238         end
0239         %end
0240         if isempty(use_ep_this_set{set_ct}),
0241             watchit(sprintf('File %s has no trials in any bins.\n',sfiles{set_ct}));
0242         end        
0243     else
0244         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0245         % Check to make sure new file is consistent with already loaded file(s)
0246         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0247         
0248         %SAMPLING RATE
0249         if (srate~=EEG.srate),
0250             error('EEG.srate in set %s does not match that of other sets.\n', ...
0251                 sfiles{set_ct});
0252         end
0253         
0254         %TIMES (in ms) FOR ALL TIME POINTS
0255         if (length(ep_times)~=length(EEG.times)),
0256             error('EEG.times in set %s does not match that of other sets.\n', ...
0257                 sfiles{set_ct});
0258         elseif max(abs(ep_times-EEG.times))
0259             error('EEG.times in set %s does not match that of other sets.\n', ...
0260                 sfiles{set_ct});
0261         end
0262         
0263         
0264         %BIN DESCRIPTORS
0265         if n_bin==length(EEG.bindesc),
0266             for b=1:n_bin,
0267                 if ~strcmpi(bindesc{b},EEG.bindesc{b}),
0268                     error('EEG.bindesc in set %s does not match that of other sets.\n', ...
0269                         sfiles{set_ct});
0270                 end
0271             end
0272         else
0273             error('EEG.bindesc in set %s does not match that of other sets.\n', ...
0274                 sfiles{set_ct});
0275         end
0276         
0277         
0278         %CHANNEL INFO
0279         if n_chan~=length(EEG.chanlocs),
0280             error('EEG.chanlocs in set %s does not match that of other sets.\n', ...
0281                 sfiles{set_ct});
0282         else
0283             for oldchan=1:length(chanlocs),
0284                 matchchan=0;
0285                 for newchan=1:length(chanlocs),
0286                     if strcmpi(chanlocs(oldchan).labels,EEG.chanlocs(newchan).labels),
0287                         matchchan=1;
0288                     end
0289                 end
0290                 if ~matchchan
0291                     error('EEG.chanlocs in set %s does not match that of other sets.\n', ...
0292                         sfiles{set_ct});
0293                 end
0294             end
0295         end
0296         
0297         %REACTION TIME INFO
0298         fldnames=fieldnames(EEG);
0299         rtmsec_attached=0;
0300         rtbins_attached=0;
0301         for d=1:length(fldnames),
0302             if strcmpi(fldnames{d},'rtmsec')
0303                 rtmsec_attached=1;
0304             elseif strcmpi(fldnames{d},'rtbins')
0305                 rtbins_attached=1;
0306             end
0307         end   
0308         new_rts_attached=rtbins_attached*rtmsec_attached;
0309         if new_rts_attached && ~rts_attached,
0310             error(['File %s has reaction time information, ' ...
0311                 'but the other file(s) you loaded do not.'],sfiles{set_ct});
0312         elseif ~new_rts_attached && rts_attached,
0313             error(['File %s does not have reaction time information, ' ...
0314                 'but the other file(s) you loaded do.'],sfiles{set_ct});
0315         end
0316        
0317         %OTHER EPOCH INFORMATION
0318         new_fldnames=fieldnames(EEG.epoch);
0319         %FIRST CHECK FIELDNAMES
0320         if length(new_fldnames)~=length(epinfo_fldnames),
0321             error(['EEG.epoch field of file %s does not match ' ...
0322                 'that of previously loaded file(s).'],sfiles{set_ct});
0323         else
0324             for a=1:length(new_fldnames),
0325                 fld_match=0;
0326                 for b=1:length(epinfo_fldnames),
0327                     if strcmpi(epinfo_fldnames{b},new_fldnames{a}),
0328                        fld_match=1;
0329                        break; %break b loop
0330                     end
0331                 end
0332                 if ~fld_match,
0333                     error(['EEG.epoch field of file %s does not match ' ...
0334                         'that of previously loaded file(s).'],sfiles{set_ct});
0335                 end
0336             end
0337         end
0338         
0339         %NOW GET EPOCHS THAT FALL IN DESIRED BINS
0340         use_ep_this_set{set_ct}=[];
0341         %if isempty(p.Results.bins), %grab all bins
0342         for a=1:length(EEG.epoch),
0343             EEG.epoch(a).sourcefile=sfiles{set_ct}; %create new field to keep
0344             %track of what file each epoch came from
0345             for j=1:length(EEG.epoch(a).eventtype), %check this epoch's types
0346                 if strcmpi(EEG.epoch(a).eventtype{j}(1:3),'bin') %is this epoch in a bin?
0347                     use_ep_this_set{set_ct}=[use_ep_this_set{set_ct} a];
0348                     grand_ep_ct=grand_ep_ct+1;
0349                     ep_info(grand_ep_ct)=EEG.epoch(a);
0350                     if rts_attached,
0351                         rtbins{grand_ep_ct}=EEG.rtbins{a};
0352                         rtmsec{grand_ep_ct}=EEG.rtmsec{a};
0353                     end
0354                     break; %leave j loop
0355                 end
0356             end
0357         end
0358         %end
0359         if isempty(use_ep_this_set{set_ct}),
0360             watchit(sprintf('File %s has no trials in any bins.\n',sfiles{set_ct}));
0361         end   
0362     end
0363 end
0364  
0365 
0366 
0367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0368 % II. Collect correlation variables and keep track of which
0369 % trials all in which bins
0370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0371 VerbReport(' ',2,VERBLEVEL);
0372 VerbReport('Identifying all possible variables for computing correlations.',2,VERBLEVEL);
0373 VerbReport('NOT considering fields EEG.event, EEG.eventccode, EEG.eventevcode, EEG.eventlogflag, nor EEG.eventlatency as they are probably not meaningful.',2,VERBLEVEL);
0374 VerbReport(' ',2,VERBLEVEL);
0375 var_ct=0;
0376 use_fields=zeros(1,length(epinfo_fldnames));
0377 %sorting_vars=cell(1,1);
0378 for a=1:length(epinfo_fldnames),
0379     use_var=1;
0380     for b=1:length(ignore_vars),
0381         if strcmpi(epinfo_fldnames{a},ignore_vars{b}),
0382             use_var=0;
0383             break; %leave b loop
0384         end
0385     end
0386     if use_var
0387         runme=['isnumeric(ep_info(1).' epinfo_fldnames{a} ')'];
0388         if eval(runme)
0389             use_fields(a)=1;
0390             VerbReport(sprintf('Using: %s',epinfo_fldnames{a}),2,VERBLEVEL);
0391             var_ct=var_ct+1;
0392             sorting_vars{var_ct}=epinfo_fldnames{a};
0393         end
0394     end
0395 end
0396 use_fields=find(use_fields);
0397 
0398 %Note: rts_attached tells you whether or not rts are loaded
0399 if rts_attached,
0400     VerbReport('Using: rtmsec',2,VERBLEVEL);
0401     var_ct=var_ct+1;
0402     sorting_vars{var_ct}='rtmsec';
0403 end
0404 VerbReport(' ',2,VERBLEVEL);
0405 
0406 %Read in all sorting variables
0407 sort_var=cell(1,length(use_fields));
0408 
0409 %preallocate memory
0410 for a=1:length(sort_var),
0411     sort_var{1,a}=zeros(1,grand_ep_ct);
0412 end
0413 bin_tracker=zeros(n_bin,grand_ep_ct);
0414 
0415 %Read values
0416 for a=1:grand_ep_ct,
0417 
0418     %get sorting variables
0419     for b=1:(length(sort_var)), %-1 because we need to ignore RTs for time being
0420         runme=['sort_var{1,b}(a)=ep_info(a).' epinfo_fldnames{use_fields(b)} ';'];
0421         eval(runme);
0422     end
0423 
0424     %track bins
0425     for c=1:length(ep_info(a).eventtype),
0426         if strcmpi(ep_info(a).eventtype{c}(1:3),'bin'), %is this a bin #
0427             bin_num=str2num(ep_info(a).eventtype{c}(4:end));
0428             bin_tracker(bin_num,a)=1;
0429         end
0430     end
0431 end
0432 
0433 
0434 
0435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0436 % III. Loop through files to get EEG data
0437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0438 %pre-allocate memory
0439 r=zeros(n_chan,length(ep_times),n_bin,length(sort_var));
0440 tau=zeros(n_chan,length(ep_times),n_bin,length(sort_var)); %Kendall's rank correlation coefficient
0441 for chan=1:n_chan,
0442     VerbReport(sprintf('\nLoading EEG from channel %s (%d of %d)',chanlocs(chan).labels, ...
0443         chan,n_chan),2,VERBLEVEL);
0444 
0445     %preallocate memory
0446     chan_dat=zeros(grand_ep_ct,length(ep_times));
0447     chan_dat_ct=0;
0448     for set_ct=1:length(sfiles),
0449         %Note, pop_loadset displays message about what file is being loaded
0450         EEG=pop_loadset(sfiles{set_ct});
0451         
0452         for d=1:length(EEG.epoch),
0453             %Grab this epoch? loop
0454             for j=1:length(EEG.epoch(d).eventtype), %check this epoch's types
0455                 if strcmpi(EEG.epoch(d).eventtype{j}(1:3),'bin') %is this type a bin?
0456                     chan_dat_ct=chan_dat_ct+1;
0457                     chan_dat(chan_dat_ct,:)=squeeze(EEG.data(chan,:,d));
0458                     break; %leave j loop
0459                 end
0460             end
0461         end %end of epoch loop
0462     end %end of file loop
0463 
0464     %compute correlations for each bin (non-RT sorting variables)
0465     for b=1:n_bin,
0466         fprintf('Bin %d\n',b);
0467         use_ep=find(bin_tracker(b,:)); %epochs that fall in this bin
0468         for s=1:length(sort_var),
0469             if isempty(use_ep)
0470                 r(chan,:,b,s)=NaN;
0471                 tau(chan,:,b,s)=NaN;
0472             else
0473                 for t=1:length(ep_times),
0474                     r(chan,t,b,s)=corr(chan_dat(use_ep,t),sort_var{s}(use_ep)');
0475                     tau(chan,t,b,s)=corr(chan_dat(use_ep,t),sort_var{s}(use_ep)', ...
0476                         'type','Kendall');
0477                 end
0478             end
0479         end
0480         
0481         %do RTs if loaded
0482         if rts_attached,
0483             if isempty(use_ep)
0484                 r(chan,:,b,s+1)=NaN;
0485                 tau(chan,:,b,s+1)=NaN;
0486             else
0487                 %get rts for this bin
0488                 rts=zeros(1,length(use_ep));
0489                 rt_ct=0;
0490                 for ep=use_ep,
0491                     rt_id=find(rtbins{ep}==b);
0492                     rt_ct=rt_ct+1;
0493                     if isempty(rt_id),
0494                         rts(rt_ct)=NaN;
0495                     else
0496                         %fprintf('Bin %d, rt_ct %d\n',b,rt_ct);
0497                         rts(rt_ct)=rtmsec{ep}(rt_id);
0498                     end
0499                 end
0500                 nonNaN=find(~isnan(rts));
0501                 if isempty(nonNaN), 
0502                     %no RTs for this bin
0503                     r(chan,:,b,s+1)=NaN;
0504                     tau(chan,:,b,s+1)=NaN;
0505                 else
0506                     for t=1:length(ep_times),
0507                         r(chan,t,b,s+1)=corr(chan_dat(use_ep(nonNaN),t),rts(nonNaN)');
0508                         tau(chan,t,b,s+1)=corr(chan_dat(use_ep(nonNaN),t),rts(nonNaN)', ...
0509                             'type','Kendall');
0510                     end
0511                 end
0512             end
0513         end
0514     end
0515 end %end of channel loop
0516 
0517 
0518 n=sum(bin_tracker,2); %keep track of sample size
0519 
0520 save(out_fname,'r','tau','n','sorting_vars','sfiles','ep_info');

Generated on Tue 10-May-2016 16:37:45 by m2html © 2005