0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
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
0131
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
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=[];
0158 GRP.grands_stder=[];
0159 GRP.grands_t=[];
0160 GRP.chanlocs=[];
0161 GRP.bin_info=[];
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;
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));
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
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));
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
0250 [sP wP]=unix(['ls ' padded]);
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
0264 infiles{s}=no_pad;
0265 end
0266 else
0267 infiles{s}=no_pad;
0268 end
0269 end
0270 end
0271 end
0272
0273
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
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
0310 if grp==1,
0311
0312 [n_chans, n_tpts, n_bins, n_subs1]=size(GND.indiv_erps);
0313
0314 if create_difs,
0315
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
0329 if ~isempty(exclude_chans),
0330 use_chans=ones(1,n_chans);
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);
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
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
0385 [n_chansNEO, n_tptsNEO, n_binsNEO, n_subsNEO]=size(GND.indiv_erps);
0386
0387
0388 if ~isempty(exclude_chans),
0389 use_chans=ones(1,n_chansNEO);
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);
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
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
0496
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
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);
0513 nB=bin_sub_ct(b,grpB);
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
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
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);
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