Home > matlabmk > GNDs2GRP.m

GNDs2GRP

PURPOSE ^

GNDs2GRP() - Creates a Mass Univariate ERP Toolbox GRP variable from a set

SYNOPSIS ^

function GRP=GNDs2GRP(gui_infiles_or_tmplt,varargin)

DESCRIPTION ^

 GNDs2GRP() - Creates a Mass Univariate ERP Toolbox GRP variable from a set
              of Mass Univariate ERP Toolbox GND variables.  A GRP variable 
              is useful for doing between-subjects analyses.  Note, all the 
              GND files need to have samples at the same points in time, 
              the same baseline window, and compatible sets of electrodes.
             
 Usage:
  >> GRP=GNDs2GRP(gui_infiles_or_tmplt,varargin);

 Required Inputs:
   gui_infiles_or_tmplt - ['gui', a cell array of strings, or a string template]
                          If 'gui', a GUI is created that allows you to select
                          which average files to import (this is probably the
                          easiest way to import files).  If a cell array of 
                          of strings, each element of the cell array should 
                          contain the filename of a GND file (e.g., 
                          {'visodbl_young.GND','visodbl_old.GND'}.  Otherwise, this 
                          input should be a filename template (i.e., a string with
                          a # where an ID number should be--'visodbl#.GND').  
                          If you provide a template, you must use the option
                          'GND_ids' (see below) to specify the ID numbers of
                          each GND file. Include the files' path unless the files 
                          are in the current working directory.

 Optional Inputs:
   group_desc       - [cell array] A string for each GND file that
                      describes that group of participants (e.g., {'young',
                      'old'}).  It might save you typing later to keep 
                      these names simple.  If this option isn't used, a 
                      pop-up window will be created for each GND file to 
                      request a name for that group. {default: not used}

   exp_desc         - [string] A string that described the experiment. If
                      not specified, the experiment descriptor from the 
                      first GND file is used. {default: not specified}

   GND_ids          - [integer vector] A set of integers specifying the
                      GND file ID numbers of each group of participants.  
                      Only necessary if a filename template is given as 
                      the input to gui_infiles_or_tmplt. {default: not
                      used}

   create_difs      - ['yes' or 'no'] If 'yes', difference waves will be
                      created for each bin in the GND files between all 
                      possible pairs of groups and added as new bins to 
                      the GRP variable. The GND files must all have the 
                      exact same bins (including bin descriptors) to use 
                      this option. {default: not used}

   use_bins         - [integer vector] A set of integers specifying which
                      bins to take difference waves.  If not specified, all 
                      bins will be imported. Note, if you import only a 
                      subset of bins, the bin numberings will start at 1 
                      and run to the number you've imported (i.e., the may
                      differ from the bin numbers in the average file. 
                      {default: import all bins}

   exclude_chans    - A cell array of channel labels to exclude from 
                      importing (e.g., {'A2','lle','rhe'}). You cannot
                      use both this option and 'include_chans' (below).{default:
                      not used, import all channels}

   include_chans    - A cell array of the labels of the channels you wish
                      to import (e.g., {'A2','lle','rhe'}).  All other 
                      channels will be ignored. You cannot use both this  
                      option and 'exclude_chans' (above). {default: not  
                      used, import all channels}

   out_fname        - [string] Filename to save GND variable to.  If empty
                      (i.e., not specified), a GUI will be created to prompt
                      you for a filename. If the string 'no save', the GND 
                      variable will not be saved to disk. 

 Output:
   GRP  - Struct variable containing grand averages derived from pairs of 
          GND variables, the file names of locations of the source GND files, 
          t-test results and more.

 Example:
   See documentation on between-participant analyses:
      http://openwetware.org/wiki/Mass_Univariate_ERP_Toolbox:_between-subject_t-tests
   or
      http://kutaslab.pbworks.com/Between_Subject_Perm_Test

 Notes:
 -Individual participant ERPs are not stored with the GRP variable.
 Rather, whenever they are needed by MATLAB, the source GND variables are
 temporarily reloaded.  Thus, it is critical for operations like the
 creation of new bins, that the source GND variable files keep the same
 file name and location as when the GRP was first created.

 Author:
 David Groppe
 Kutaslab, 3/2010

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % GNDs2GRP() - Creates a Mass Univariate ERP Toolbox GRP variable from a set
0002 %              of Mass Univariate ERP Toolbox GND variables.  A GRP variable
0003 %              is useful for doing between-subjects analyses.  Note, all the
0004 %              GND files need to have samples at the same points in time,
0005 %              the same baseline window, and compatible sets of electrodes.
0006 %
0007 % Usage:
0008 %  >> GRP=GNDs2GRP(gui_infiles_or_tmplt,varargin);
0009 %
0010 % Required Inputs:
0011 %   gui_infiles_or_tmplt - ['gui', a cell array of strings, or a string template]
0012 %                          If 'gui', a GUI is created that allows you to select
0013 %                          which average files to import (this is probably the
0014 %                          easiest way to import files).  If a cell array of
0015 %                          of strings, each element of the cell array should
0016 %                          contain the filename of a GND file (e.g.,
0017 %                          {'visodbl_young.GND','visodbl_old.GND'}.  Otherwise, this
0018 %                          input should be a filename template (i.e., a string with
0019 %                          a # where an ID number should be--'visodbl#.GND').
0020 %                          If you provide a template, you must use the option
0021 %                          'GND_ids' (see below) to specify the ID numbers of
0022 %                          each GND file. Include the files' path unless the files
0023 %                          are in the current working directory.
0024 %
0025 % Optional Inputs:
0026 %   group_desc       - [cell array] A string for each GND file that
0027 %                      describes that group of participants (e.g., {'young',
0028 %                      'old'}).  It might save you typing later to keep
0029 %                      these names simple.  If this option isn't used, a
0030 %                      pop-up window will be created for each GND file to
0031 %                      request a name for that group. {default: not used}
0032 %
0033 %   exp_desc         - [string] A string that described the experiment. If
0034 %                      not specified, the experiment descriptor from the
0035 %                      first GND file is used. {default: not specified}
0036 %
0037 %   GND_ids          - [integer vector] A set of integers specifying the
0038 %                      GND file ID numbers of each group of participants.
0039 %                      Only necessary if a filename template is given as
0040 %                      the input to gui_infiles_or_tmplt. {default: not
0041 %                      used}
0042 %
0043 %   create_difs      - ['yes' or 'no'] If 'yes', difference waves will be
0044 %                      created for each bin in the GND files between all
0045 %                      possible pairs of groups and added as new bins to
0046 %                      the GRP variable. The GND files must all have the
0047 %                      exact same bins (including bin descriptors) to use
0048 %                      this option. {default: not used}
0049 %
0050 %   use_bins         - [integer vector] A set of integers specifying which
0051 %                      bins to take difference waves.  If not specified, all
0052 %                      bins will be imported. Note, if you import only a
0053 %                      subset of bins, the bin numberings will start at 1
0054 %                      and run to the number you've imported (i.e., the may
0055 %                      differ from the bin numbers in the average file.
0056 %                      {default: import all bins}
0057 %
0058 %   exclude_chans    - A cell array of channel labels to exclude from
0059 %                      importing (e.g., {'A2','lle','rhe'}). You cannot
0060 %                      use both this option and 'include_chans' (below).{default:
0061 %                      not used, import all channels}
0062 %
0063 %   include_chans    - A cell array of the labels of the channels you wish
0064 %                      to import (e.g., {'A2','lle','rhe'}).  All other
0065 %                      channels will be ignored. You cannot use both this
0066 %                      option and 'exclude_chans' (above). {default: not
0067 %                      used, import all channels}
0068 %
0069 %   out_fname        - [string] Filename to save GND variable to.  If empty
0070 %                      (i.e., not specified), a GUI will be created to prompt
0071 %                      you for a filename. If the string 'no save', the GND
0072 %                      variable will not be saved to disk.
0073 %
0074 % Output:
0075 %   GRP  - Struct variable containing grand averages derived from pairs of
0076 %          GND variables, the file names of locations of the source GND files,
0077 %          t-test results and more.
0078 %
0079 % Example:
0080 %   See documentation on between-participant analyses:
0081 %      http://openwetware.org/wiki/Mass_Univariate_ERP_Toolbox:_between-subject_t-tests
0082 %   or
0083 %      http://kutaslab.pbworks.com/Between_Subject_Perm_Test
0084 %
0085 % Notes:
0086 % -Individual participant ERPs are not stored with the GRP variable.
0087 % Rather, whenever they are needed by MATLAB, the source GND variables are
0088 % temporarily reloaded.  Thus, it is critical for operations like the
0089 % creation of new bins, that the source GND variable files keep the same
0090 % file name and location as when the GRP was first created.
0091 %
0092 % Author:
0093 % David Groppe
0094 % Kutaslab, 3/2010
0095 
0096 %%%%%%%%%%%%%%%% REVISION LOG %%%%%%%%%%%%%%%%%
0097 % 5/7/2010-GRP.perm_tests turned into GRP.t_tests to be compatible with FDR
0098 % code
0099 %
0100 % 10/25/2010-Modified to be able to deal with GND variables with no cal
0101 % pulse information.  If a single GND variable does not have cal pulse
0102 % information, the GRP variable will not have cal pulse information
0103 % (GRP.cals will be empty)
0104 %
0105 % 4/4/2011: Made compatible with Windows.
0106 %
0107 
0108 %%%%%%%%%%%%%%%% FUTURE WORK %%%%%%%%%%%%%%%%%
0109 % -Add Verblevel optional input?
0110 % -Would it be useful to add the field GRP.sub_ct?
0111 % -Make sure one can write Kutaslab avg files from a GRP variable
0112 % -Is it really useful to have cal pulse information in a GRP variable?
0113 % -Would it be useful to have the field GRP.indiv_bin_erps?
0114 
0115 function GRP=GNDs2GRP(gui_infiles_or_tmplt,varargin)
0116 
0117 p=inputParser;
0118 p.addRequired('gui_infiles_or_tmplt',@(x) ischar(x) || iscell(x));
0119 p.addParamValue('GND_ids',[],@isnumeric);
0120 p.addParamValue('use_bins',[],@isnumeric);
0121 p.addParamValue('exclude_chans',[],@(x) ischar(x) || iscell(x)); 
0122 p.addParamValue('include_chans',[],@(x) ischar(x) || iscell(x));
0123 p.addParamValue('out_fname',[],@ischar);
0124 p.addParamValue('exp_desc',[],@ischar);
0125 p.addParamValue('group_desc',[],@iscell);
0126 p.addParamValue('create_difs','no',@(x) ischar(x) && (strcmpi(x,'no') || strcmpi(x,'yes')) );
0127 p.parse(gui_infiles_or_tmplt,varargin{:});
0128 
0129 
0130 %Figure out which channels to ignore if any
0131 %But first make sure exclude & include options were not both used.
0132 if ~isempty(p.Results.include_chans) && ~isempty(p.Results.exclude_chans)
0133     error('You cannot use BOTH ''include_chans'' and ''exclude_chans'' options.');
0134 end
0135 if ischar(p.Results.exclude_chans),
0136     exclude_chans{1}=p.Results.exclude_chans;
0137 elseif isempty(p.Results.exclude_chans)
0138     exclude_chans=[];
0139 else
0140     exclude_chans=p.Results.exclude_chans;
0141 end
0142 if ischar(p.Results.include_chans),
0143     include_chans{1}=p.Results.include_chans;
0144 elseif isempty(p.Results.include_chans)
0145     include_chans=[];
0146 else
0147     include_chans=p.Results.include_chans;
0148 end
0149 
0150 %Initialize GRP
0151 GRP.exp_desc=p.Results.exp_desc;
0152 GRP.filename=[];
0153 GRP.filepath=[];
0154 GRP.saved='no';
0155 GRP.group_desc=[];
0156 GRP.GND_fnames=[];
0157 GRP.grands=[];   % <-grand average ERPs (chan x time point x  bin)
0158 GRP.grands_stder=[]; % <-standard error of grand average ERPs (chan x time point x  bin)
0159 GRP.grands_t=[]; %<-grand average ERPs as t-scores (chan x time point x  bin)
0160 GRP.chanlocs=[];
0161 GRP.bin_info=[]; %<-stores information about how difference waves were derived (which groups and bins)
0162 GRP.condesc=[];
0163 GRP.time_pts=[];
0164 GRP.bsln_wind=[];
0165 GRP.odelay=[];
0166 GRP.srate=[];
0167 GRP.indiv_fnames=[];
0168 GRP.indiv_subnames=[];
0169 GRP.indiv_traits=[];
0170 GRP.indiv_bin_ct=[];
0171 GRP.indiv_bin_raw_ct=[];
0172 GRP.indiv_art_ics=[];
0173 GRP.cals.indiv_cals=[]; 
0174 GRP.cals.indiv_cal_ct=[];
0175 GRP.cals.grand_cals=[];
0176 GRP.cals.caldesc=[]; 
0177 GRP.cals.condcode=0;
0178 GRP.cals.condesc=[];
0179 GRP.history=[];
0180 GRP.t_tests=[];
0181 
0182 
0183 if strcmpi(gui_infiles_or_tmplt,'GUI'),
0184     loading=1;
0185     infiles=[];
0186     while loading,
0187         fprintf('Select GND files to import.\n');
0188         fprintf('To select multiple contiguous files in the same directory, hold down the shift key when selecting.\n');
0189         fprintf('To select files in multiple directories or non-contiguous files in the same direcotry,\n');
0190         fprintf('select all the files in one directory and hit the "Open" button.\n');
0191         fprintf('You will then be given the opportunity to load more files from the current or another directory.\n');
0192         [neofname, inpath]=uigetfile({'*.GND','*.GND files'; ...
0193             '*.*','All Files (*.*)'},'Mass Uni GND Files to Import','MultiSelect','on');
0194         if ischar(neofname),
0195             clear infname;
0196             infname{1}=neofname; %make it a cell array for consistent syntax below
0197         else
0198             infname=neofname;
0199         end
0200         if ~inpath,
0201             if isempty(infiles),
0202                 fprintf('File selection cancelled.  Aborting GNDs2GRP.\n');
0203                 GRP=[];
0204                 return;
0205             else
0206                 loading=0;
0207             end
0208         else
0209             if isempty(infiles),
0210                 infiles=cell(1,length(infname)); %preallocate mem
0211                 for a=1:length(infname),
0212                     infiles{a}=[inpath infname{a}];
0213                 end
0214             else
0215                 n_files=length(infiles);
0216                 ct=0;
0217                 for a=(n_files+1):(n_files+length(infname)),
0218                     ct=ct+1;
0219                     infiles{a}=[inpath infname{ct}];
0220                 end
0221             end
0222             % trigger GUI to see if user wants to load more
0223             resp=questdlg('Do you want to load more files?',...
0224                 'OUTPUT','Yes','No','Yes');
0225             if strcmpi(resp,'No'),
0226                 loading=0;
0227             end
0228         end
0229     end
0230 elseif iscell(p.Results.gui_infiles_or_tmplt)
0231     infiles=p.Results.gui_infiles_or_tmplt;
0232 else
0233     if isempty(p.Results.GND_ids)
0234         error('If gui_infiles_or_tmplt is a string filename template, you must specify the GND file numbers with the argument ''GND_ids''.');
0235     elseif ~ismember('#',p.Results.gui_infiles_or_tmplt),
0236         error('The filename template %s needs to contain a # to indicate where the GND file numbers go (e.g., yngvob#.GND).', ...
0237             p.Results.gui_infiles_or_tmplt);
0238     else
0239         lb_id=find(p.Results.gui_infiles_or_tmplt=='#');
0240         prefix=p.Results.gui_infiles_or_tmplt(1:(lb_id(1)-1)); %indexing lb_id by 1 in case there are multiple pound signs
0241         postfix=p.Results.gui_infiles_or_tmplt((lb_id(1)+1):end);
0242         n_subs=length(p.Results.GND_ids);
0243         infiles=cell(1,n_subs);
0244         for s=1:n_subs,
0245             no_pad=[prefix num2str(p.Results.GND_ids(s)) postfix];
0246             if p.Results.GND_ids(s)<10,
0247                 padded=[prefix num2str(p.Results.GND_ids(s),'%.2d') postfix];
0248                 if ismac || isunix
0249                     %Mac or Unix OS
0250                     [sP wP]=unix(['ls ' padded]); %if sp==0, then the file exists, need wP argument so that it isn't automatically displayed in command window
0251                     [sNP wNP]=unix(['ls ' no_pad]);
0252                     if ~sP
0253                         if ~sNP,
0254                             error('You have a file named %s and one named %s.  I don''t know which one to load!\n', ...
0255                                 padded,no_pad);
0256                         else
0257                             infiles{s}=padded;
0258                         end
0259                     else
0260                         infiles{s}=no_pad;
0261                     end
0262                 else
0263                     %Must be a PC
0264                     infiles{s}=no_pad;
0265                 end
0266             else
0267                 infiles{s}=no_pad;
0268             end
0269         end
0270     end
0271 end
0272 
0273 % Add group descriptors if specified
0274 if ~isempty(p.Results.group_desc),
0275     if length(p.Results.group_desc)~=length(infiles),
0276         error('The number of group descriptors needs to equal the number of GND files.');
0277     else
0278         GRP.group_desc=p.Results.group_desc;
0279     end
0280 end
0281 
0282 if strcmpi(p.Results.create_difs,'yes'),
0283     create_difs=1;
0284 else
0285     create_difs=0;
0286 end
0287 
0288 GRP.GND_fnames=infiles;
0289 n_groups=length(infiles);
0290 if n_groups<2,
0291    error('You need at least two GND files to make a GRP variable.'); 
0292 end
0293 
0294 for grp=1:n_groups,
0295     
0296     %load new GND variable
0297     GND=[];
0298     load(infiles{grp},'-MAT');
0299     if isempty(GND),
0300        error('File %s needs to contain a Mass Univariate ERP Toolbox GND variable that is called "GND"',infiles{grp}); 
0301     end
0302        
0303     if isempty(p.Results.group_desc),
0304        resp=inputdlg(sprintf('Enter the group descriptor for the participants in file %s (e.g., patients).', ...
0305            infiles{grp}),'Name Group of Participants');
0306        GRP.group_desc{grp}=resp{1};
0307     end
0308     
0309     %Set group general fields/check for GND field compatibility
0310     if grp==1,
0311         %set group general fields
0312         [n_chans, n_tpts, n_bins, n_subs1]=size(GND.indiv_erps);
0313         
0314         if create_difs,
0315             % select bins (in case only a subset were requested)
0316             if isempty(p.Results.use_bins),
0317                 use_bins=1:n_bins;
0318             else
0319                 use_bins=p.Results.use_bins;
0320             end
0321             n_use_bins=length(use_bins);
0322         end
0323             
0324         if isempty(GRP.exp_desc),
0325             GRP.exp_desc=GND.exp_desc;
0326         end        
0327         
0328         % select channels (in case only a subset were requested)
0329         if ~isempty(exclude_chans),
0330             use_chans=ones(1,n_chans); %preallocate mem
0331             for c=1:n_chans,
0332                 for a=1:length(exclude_chans)
0333                     if strcmpi(GND.chanlocs(c).labels,exclude_chans{a}),
0334                         use_chans(c)=0;
0335                         break;
0336                     end
0337                 end
0338             end
0339             use_chans=find(use_chans>0);
0340         elseif ~isempty(include_chans),
0341             use_chans=zeros(1,n_chans); %preallocate mem
0342             for c=1:n_chans,
0343                 for a=1:length(include_chans)
0344                     if strcmpi(GND.chanlocs(c).labels,include_chans{a}),
0345                         use_chans(c)=1;
0346                         break;
0347                     end
0348                 end
0349             end
0350             use_chans=find(use_chans>0);
0351         else
0352             use_chans=1:n_chans;
0353         end
0354         GRP.chanlocs=GND.chanlocs(use_chans);
0355         
0356         if create_difs,
0357             GRP.condesc=GND.condesc;
0358         end
0359         GRP.time_pts=GND.time_pts;
0360         GRP.bsln_wind=GND.bsln_wind;
0361         GRP.odelay=GND.odelay;
0362         GRP.srate=GND.srate;
0363         if isempty(GND.cals),
0364             GRP.cals=[];
0365         else
0366             GRP.cals.grand_cals=zeros(length(use_chans),n_tpts,n_groups);
0367             GRP.cals.caldesc=GND.cals.caldesc;
0368             GRP.cals.condesc=GND.cals.condesc;
0369         end
0370         
0371         if create_difs,
0372             %preallocate mem
0373             bin_sub_ct=zeros(n_use_bins,n_groups);
0374             bindesc=cell(1,n_use_bins);
0375             bin_condcodes=zeros(1,n_use_bins);
0376             bin_ct=0;
0377             for b=use_bins,
0378                 bin_ct=bin_ct+1;
0379                 bindesc{bin_ct}=GND.bin_info(b).bindesc;
0380                 bin_condcodes(bin_ct)=GND.bin_info(b).condcode;
0381             end
0382         end
0383     else
0384         %check for compatibility with previously loaded GND variables
0385         [n_chansNEO, n_tptsNEO, n_binsNEO, n_subsNEO]=size(GND.indiv_erps);
0386                 
0387         % select channels (in case only a subset were requested)
0388         if ~isempty(exclude_chans),
0389             use_chans=ones(1,n_chansNEO); %preallocate mem
0390             for c=1:n_chansNEO,
0391                 for a=1:length(exclude_chans),
0392                     if strcmpi(GND.chanlocs(c).labels,exclude_chans{a}),
0393                         use_chans(c)=0;
0394                         break;
0395                     end
0396                 end
0397             end
0398             use_chans=find(use_chans>0);
0399         elseif ~isempty(include_chans),
0400             use_chans=zeros(1,n_chansNEO); %preallocate mem
0401             for c=1:n_chansNEO,
0402                 for a=1:length(include_chans)
0403                     if strcmpi(GND.chanlocs(c).labels,include_chans{a}),
0404                         use_chans(c)=1;
0405                         break;
0406                     end
0407                 end
0408             end
0409             use_chans=find(use_chans>0);
0410         else
0411             use_chans=1:n_chansNEO;
0412         end
0413         
0414         %Make sure new GND variable in consistent with the old
0415         if ~isequal(GND.chanlocs(use_chans),GRP.chanlocs),
0416             error('The channel location information in the GND variable from file %s differs from that in previous files.', ...
0417                 infiles{grp});
0418         end
0419         
0420         if create_difs,
0421             if ~isequal(GND.condesc,GRP.condesc),
0422                 error('The condition code descriptor(s) in the GND variable from file %s differs from those in previous files.', ...
0423                     infiles{grp});
0424             end
0425         end
0426           
0427         if ~isequal(GND.time_pts,GRP.time_pts),
0428             error('The time points in the GND variable from file %s differ from those in previous files.', ...
0429                 infiles{grp});
0430         end
0431         
0432         if ~isequal(GND.bsln_wind,GRP.bsln_wind),
0433             error('The baseline time window in the GND variable from file %s differs from that in previous files.', ...
0434                 infiles{grp});
0435         end
0436         
0437         if ~isequal(GND.odelay,GRP.odelay),
0438             error('The odelay in the GND variable from file %s differs from that in previous files.', ...
0439                 infiles{grp});
0440         end
0441         
0442         if ~isequal(GND.srate,GRP.srate),
0443             error('The sampling rate in the GND variable from file %s differs from that in previous files.', ...
0444                 infiles{grp});
0445         end
0446         
0447         if ~isempty(GND.cals) && ~strcmpi(GND.cals.caldesc,GRP.cals.caldesc)
0448             watchit(sprintf('The cal pulse bin descriptor in the GND variable from file %s differs from that in previous files.', ...
0449                 infiles{grp}));
0450         end
0451         
0452         if ~isempty(GND.cals) && ~strcmpi(GND.cals.condesc,GRP.cals.condesc)
0453             watchit(sprintf('The cal pulse condition code descriptor in the GND variable from file %s differs from that in previous files.', ...
0454                 infiles{grp}));
0455         end
0456                
0457         if create_difs,
0458             bin_ct=0;
0459             for b=use_bins,
0460                 bin_ct=bin_ct+1;
0461                 if ~strcmpi(bindesc{bin_ct},GND.bin_info(b).bindesc),
0462                     watchit('Bin descriptor for Bin %d in file %s, differs from that in previous file(s).', ...
0463                         b,infiles{grp});
0464                     fprintf('Bin descriptor for Bin %d, file %s is %s.\n',b,infiles{grp},GND.bin_info(b).bindesc)
0465                     fprintf('Bin descriptor for Bin %d, previous file(s) is %s. Using bin descriptor from previous file(s).\n',b,bindesc{bin_ct});
0466                 end
0467                 if bin_condcodes(bin_ct)~=GND.bin_info(b).condcode,
0468                     watchit('Condition code for Bin %d in file %s is %d, but in previous file(s) it is %d.', ...
0469                         b,infiles{grp},GND.bin_info(b).condcode,bin_condcodes(bin_ct));
0470                     fprintf('Using condition code from previous file(s).\n')
0471                 end
0472             end
0473         end
0474     end
0475     if ~isempty(GND.cals) && (GND.cals.condcode~=0)
0476         watchit(sprintf('The cal pulse condition code in the GND variable from file %s is not 0.\nThe cal pulse condition code will be set to 0 in the GRP variable.\n', ...
0477             infiles{grp}));
0478     end
0479     
0480   
0481     GRP.indiv_fnames{grp}=GND.indiv_fnames;
0482     GRP.indiv_subnames{grp}=GND.indiv_subnames;
0483     GRP.indiv_traits{grp}=GND.indiv_traits;
0484     GRP.indiv_art_ics{grp}=GND.indiv_art_ics;
0485     if ~isempty(GND.cals),
0486         GRP.cals.indiv_cals{grp}=GND.cals.indiv_cals(use_chans,:,:);
0487         GRP.cals.indiv_cal_ct{grp}=GND.cals.indiv_cal_ct;
0488         GRP.cals.grand_cals(:,:,grp)=GND.cals.grand_cals(use_chans,:);
0489     end
0490     
0491     if create_difs,
0492         bin_sub_ct(:,grp)=GND.sub_ct(use_bins);
0493         GRP.indiv_bin_ct{grp}=GND.indiv_bin_ct(:,use_bins);
0494         GRP.indiv_bin_raw_ct{grp}=GND.indiv_bin_raw_ct(:,use_bins);
0495         % Grab ERPs from whole group and compute sum of squares (SS) for each
0496         % bin
0497         group_grands(:,:,:,grp)=GND.grands(use_chans,:,use_bins);
0498         n_subs=zeros(1,1,n_use_bins);
0499         n_subs(1,1,:)=GND.sub_ct(use_bins);
0500         n_subs=repmat(n_subs,[length(use_chans) n_tpts 1]);
0501         group_ss(:,:,:,grp)=(GND.grands_stder(use_chans,:,use_bins).^2).*n_subs.*(n_subs-1);
0502     end
0503 end
0504 
0505 if create_difs,
0506     % Compute grand average difference waves & t-scores
0507     dif_ct=0;
0508     for grpA=1:n_groups,
0509         for grpB=(grpA+1):n_groups,
0510             for b=1:n_use_bins,
0511                 dif_ct=dif_ct+1;
0512                 nA=bin_sub_ct(b,grpA); % # of participants who contributed to this bin from Group A
0513                 nB=bin_sub_ct(b,grpB); % # of participants who contributed to this bin from Group B
0514                 GRP.grands(:,:,dif_ct)=group_grands(:,:,b,grpA)-group_grands(:,:,b,grpB);
0515                 SS_pooled=(group_ss(:,:,b,grpA)+group_ss(:,:,b,grpB))/(nA+nB-2);
0516                 GRP.grands_stder(:,:,dif_ct)=sqrt( SS_pooled*(nA+nB)/(nA*nB) );
0517                 
0518                 % describe difference in bin info
0519                 GRP.bin_info(b).bindesc=sprintf('%s (%s-%s)',bindesc{b}, ...
0520                     GRP.group_desc{grpA},GRP.group_desc{grpB});
0521                 GRP.bin_info(b).condcode=bin_condcodes(b);
0522                 GRP.bin_info(b).groupA=grpA;
0523                 GRP.bin_info(b).groupB=grpB;
0524                 GRP.bin_info(b).source_binA=b;
0525                 GRP.bin_info(b).source_binB=b;
0526                 GRP.bin_info(b).n_subsA=nA;
0527                 GRP.bin_info(b).n_subsB=nB;
0528                 GRP.bin_info(b).op='A-B';
0529             end
0530         end
0531     end
0532     GRP.grands_t=GRP.grands./GRP.grands_stder;
0533 end
0534 
0535 if isempty(p.Results.out_fname),
0536     %Create GUI
0537     [jname, jpath]=uiputfile({'*.GRP','*.GRP files'; ...
0538         '*.*','All files'},'Save GRP variable as:','untitled.GRP');
0539     if ~jpath,
0540         fprintf('Output filename selection cancelled.  GRP variable NOT saved to disk.\n');
0541     else
0542         GRP=save_matmk(GRP,jname,jpath,1); % 1 means that user won't be asked again about saving file
0543     end 
0544 elseif ~strcmpi(p.Results.out_fname,'no save'),
0545     [jpath, jname]=pathNname(p.Results.out_fname);
0546     GRP=save_matmk(GRP,jname,jpath);
0547 end

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