0001 function GND=gui_avgs2GND()
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
0116
0117
0118 [infname, inpath]=uigetfile({'*.mas','*.mas files'; ...
0119 '*.cms','*.cms files'; ...
0120 '*.nrm','*.nrm files'; ...
0121 '*.cnm','*.cnm files'; ...
0122 '*.iccor',' *.iccor files'; ...
0123 '*.*','All Files (*.*)'},'First select Kutaslab average files to import','MultiSelect','on');
0124 if ~inpath,
0125 fprintf('File selection cancelled. Aborting gui_avgs2GND.\n');
0126 GND=[];
0127 return;
0128 else
0129 infiles=cell(1,length(infname));
0130 for a=1:length(infname),
0131 infiles{a}=[inpath infname{a}];
0132 end
0133 end
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144 n_subs=length(infiles);
0145 for sub=1:n_subs,
0146
0147
0148 fprintf('Opening file %s\n',infiles{sub});
0149 fid=erpio('openavg',infiles{sub});
0150 if (fid==-1)
0151 msg=erpio('get_errstr');
0152 error('Could not open file %s using erpio. According to erpio: %s', ...
0153 infiles{sub},msg);
0154 end
0155
0156
0157 n_chans=erpio('get_hdrvar',fid,'chans',1);
0158 use_chans=zeros(1,n_chans);
0159 used_chan_ct=0;
0160
0161 if ~isempty(p.Results.locfile) || (sub>1),
0162 for c=1:n_chans,
0163 chan_label=erpio('get_hdrvar',fid,'chndesc',c-1,1);
0164 if ismember(chan_label,use_chan_labels),
0165 used_chan_ct=used_chan_ct+1;
0166 if ~strcmpi(GND.chanlocs(used_chan_ct).labels,chan_label),
0167 erpio('close',fid);
0168 error('In %s, the #%d used channel is %s. In %s, it is %s.\n', ...
0169 infiles{sub},used_chan_ct,chan_label,locfile,GND.chanlocs(used_chan_ct).labels);
0170 else
0171 use_chans(c)=1;
0172 end
0173 end
0174 end
0175 if sum(use_chans)~=n_use_chans,
0176 erpio('close',fid);
0177 error('File %s has a different number of channels than all previous files.', ...
0178 infiles{sub});
0179 end
0180 else
0181
0182
0183 if n_chans>=59,
0184
0185 load('/usr/local/matlab-toolboxes/matlabmk/mk66locs');
0186 else
0187
0188 load('/usr/local/matlab-toolboxes/matlabmk/mk33locs');
0189 end
0190 n_chanlocs=length(chanlocs);
0191 for c=1:n_chans,
0192 chan_label=erpio('get_hdrvar',fid,'chndesc',c-1,1);
0193 use_chans(c)=1;
0194 if ~isempty(exclude_chans) && ismember(chan_label,exclude_chans),
0195 use_chans(c)=0;
0196 end
0197 if ~isempty(include_chans) && ~ismember(chan_label,include_chans),
0198 use_chans(c)=0;
0199 end
0200 if use_chans(c),
0201 used_chan_ct=used_chan_ct+1;
0202
0203 if strcmpi(chan_label,'lle'),
0204 avg_label='LLEy';
0205 elseif strcmpi(chan_label,'rle'),
0206 avg_label='RLEy';
0207 elseif strcmpi(chan_label,'lhe') || strcmpi(chan_label,'lhz'),
0208 avg_label='LHEy';
0209 elseif strcmpi(chan_label,'rhe') || strcmpi(chan_label,'rhz'),
0210 avg_label='RHEy';
0211 elseif strcmpi(chan_label,'HE'),
0212 avg_label='HEOG';
0213 elseif strcmpi(chan_label,'VE'),
0214 avg_label='VEOG';
0215 else
0216 avg_label=chan_label;
0217 end
0218 found=0;
0219 for d=1:n_chanlocs,
0220 if strcmpi(avg_label,chanlocs(d).labels),
0221 if used_chan_ct==1,
0222 GND.chanlocs=chanlocs(d);
0223 else
0224 GND.chanlocs(used_chan_ct)=chanlocs(d);
0225 end
0226 GND.chanlocs(used_chan_ct).labels=chan_label;
0227 found=1;
0228 end
0229 end
0230 if ~found,
0231 watchit(sprintf(['Could not find a default coordinate for channel %s. ' ...
0232 'Giving it the bogus coordinate of a few inches in front of the nose.\n'],chan_label));
0233 GND.chanlocs(used_chan_ct)=chanlocs(n_chanlocs);
0234 GND.chanlocs(used_chan_ct).labels=chan_label;
0235 end
0236 end
0237 end
0238 n_use_chans=used_chan_ct;
0239 use_chan_labels=cell(1,n_use_chans);
0240 for d=1:n_use_chans,
0241 use_chan_labels{d}=GND.chanlocs(d).labels;
0242 end
0243 end
0244
0245
0246 n_bins=erpio('getnbins',fid);
0247 if ~isempty(GND.indiv_erps),
0248 if n_bins~=total_psbl_bins,
0249 erpio('close',fid);
0250 error('File %s has %d bins. The previous files have %d bins (counting Bin 0, which should contain cal pulses).', ...
0251 infiles{sub},n_bins,total_psbl_bins);
0252 end
0253 end
0254 if sub==1,
0255 if isempty(p.Results.use_bins),
0256 use_bins=1:(n_bins-1);
0257 else
0258 use_bins=p.Results.use_bins;
0259 end
0260 total_psbl_bins=n_bins;
0261 end
0262
0263
0264 if isempty(GND.exp_desc),
0265 GND.exp_desc=erpio('get_hdrvar',fid,'expdesc',1);
0266 else
0267 neo_desc=erpio('get_hdrvar',fid,'expdesc',1);
0268 if ~strcmpi(GND.exp_desc,neo_desc),
0269 watchit(sprintf('The experiment descriptor for file %s is %s.\nUsing initial descriptor: %s.\n', ...
0270 infiles{sub},neo_desc,GND.exp_desc));
0271 end
0272 end
0273
0274
0275 if isempty(GND.bindesc),
0276
0277 bin_ct=0;
0278 for b=use_bins,
0279 bin_ct=bin_ct+1;
0280 GND.bindesc{bin_ct}=erpio('get_hdrvar',fid,'bindesc',b);
0281 end
0282 GND.cals.caldesc=erpio('get_hdrvar',fid,'bindesc',0);
0283 else
0284
0285 bin_ct=0;
0286 for b=use_bins,
0287 bin_ct=bin_ct+1;
0288 neo_bindesc=erpio('get_hdrvar',fid,'bindesc',b);
0289 if ~strcmpi(GND.bindesc{bin_ct},neo_bindesc),
0290 erpio('close',fid);
0291 error('The bin descriptor for file %s, Bin %d is %s. For previous file(s) it is %s.', ...
0292 infiles{sub},b,neo_bindesc,GND.bindesc{bin_ct});
0293 end
0294 end
0295
0296 neo_bindesc=erpio('get_hdrvar',fid,'bindesc',0);
0297 if ~strcmpi(GND.cals.caldesc,neo_bindesc),
0298 erpio('close',fid);
0299 error('The bin descriptor for file %s, Bin %d is %s. For previous file(s) it is %s.', ...
0300 infiles{sub},0,neo_bindesc,GND.cals.caldesc);
0301 end
0302 end
0303
0304
0305 if isempty(GND.condcode),
0306
0307 bin_ct=0;
0308 for b=use_bins,
0309 bin_ct=bin_ct+1;
0310 GND.condcode(bin_ct)=erpio('get_hdrvar',fid,'condcode',b);
0311 end
0312 GND.cals.condcode=erpio('get_hdrvar',fid,'condcode',0);
0313 else
0314
0315 bin_ct=0;
0316 for b=use_bins,
0317 bin_ct=bin_ct+1;
0318 neo_ccode=erpio('get_hdrvar',fid,'condcode',b);
0319 if neo_ccode~=GND.condcode(bin_ct),
0320 erpio('close',fid);
0321 error('The condition code for Bin %d in file %s is %d. In previous file(s) it is %d.', ...
0322 b,infiles{sub},neo_ccode,GND.condcode(bin_ct));
0323 end
0324 end
0325
0326 neo_ccode=erpio('get_hdrvar',fid,'condcode',0);
0327 if neo_ccode~=GND.cals.condcode,
0328 erpio('close',fid);
0329 error('The condition code for Bin 0 in file %s is %d. In previous file(s) it is %d.', ...
0330 infiles{sub},neo_ccode,GND.cals.condcode);
0331 end
0332 end
0333
0334
0335 if isempty(GND.condesc),
0336
0337
0338
0339 GND.cals.condesc=erpio('get_hdrvar',fid,'condesc',0);
0340
0341 if sum(use_bins>1),
0342
0343 n_ccode=1;
0344 GND.condesc{n_ccode}=erpio('get_hdrvar',fid,'condesc',use_bins(1));
0345 if length(use_bins)>1,
0346 for b=use_bins(2:end),
0347 neo_ccdesc=erpio('get_hdrvar',fid,'condesc',b);
0348 if ~strcmpi(neo_ccdesc,GND.condesc{n_ccode}),
0349 n_ccode=n_ccode+1;
0350 GND.condesc{n_ccode}=neo_ccdesc;
0351 end
0352 end
0353 end
0354 end
0355 else
0356
0357 bin_ct=0;
0358 for b=use_bins,
0359 bin_ct=bin_ct+1;
0360 neo_condesc=erpio('get_hdrvar',fid,'condesc',b);
0361 if ~strcmpi(GND.condesc{GND.condcode(bin_ct)},neo_condesc),
0362 erpio('close',fid);
0363 error('The condition code descriptor for file %s, Bin %d is %s. For previous file(s) it is %s.', ...
0364 infiles{sub},b,neo_condesc,GND.condesc{GND.condcode(bin_ct)});
0365 end
0366 end
0367
0368 neo_condesc=erpio('get_hdrvar',fid,'condesc',0);
0369 if ~strcmpi(GND.cals.condesc,neo_condesc),
0370 erpio('close',fid);
0371 error('The condition code descriptor for file %s, Bin %d is %s. For previous file(s) it is %s.', ...
0372 infiles{sub},0,neo_condesc,GND.cals.condesc);
0373 end
0374 end
0375
0376
0377
0378 cprecis=erpio('get_hdrvar',fid,'cprecis',1);
0379 presamp=erpio('get_hdrvar',fid,'presampling',1);
0380 srate=100000/erpio('get_hdrvar',fid,'clktick',1);
0381 GND.srate=srate;
0382 if isempty(GND.time_pts),
0383 n_pts=cprecis*256;
0384 GND.time_pts=[0:(n_pts-1)]*1000/srate-presamp;
0385 if isempty(p.Results.bsln),
0386 bsln=[-presamp -1000/srate];
0387 fprintf('Using default baseline of %d to %d ms ', ...
0388 -presamp,-1000/srate);
0389 else
0390 bsln=p.Results.bsln;
0391 fprintf('Using baseline of %d to %d ms ', ...
0392 bsln(1),bsln(2));
0393 end
0394 bsln_pts=[find_tpt(bsln(1),GND.time_pts):find_tpt(bsln(2),GND.time_pts)];
0395 fprintf('(%d to %d in time points)\n',bsln_pts(1),bsln_pts(end));
0396 else
0397 cprecis=erpio('get_hdrvar',fid,'cprecis',1);
0398 presamp=erpio('get_hdrvar',fid,'presampling',1);
0399 srate=100000/erpio('get_hdrvar',fid,'clktick',1);
0400 neo_pts=cprecis*256;
0401 if neo_pts~=n_pts,
0402 erpio('close',fid);
0403 error('File %s has %d time points of EEG. Previous file(s) have %d.', ...
0404 infiles{sub},neo_pts,n_pts);
0405 end
0406 neo_time_pts=[0:(n_pts-1)]*1000/srate-presamp;
0407 if sum(neo_time_pts~=GND.time_pts),
0408 erpio('close',fid);
0409 error('File %s has either a different sampling rate or prestimulus baseline than previous file(s).', ...
0410 infiles{sub});
0411 end
0412 end
0413
0414
0415 if isempty(GND.odelay),
0416 GND.odelay=erpio('get_hdrvar',fid,'odelay',1);
0417 else
0418 odelay=erpio('get_hdrvar',fid,'odelay',1);
0419 if GND.odelay~=odelay,
0420 error('File %s has an odelay of %d. Previous files have an odelay %d.', ...
0421 odelay,GND.odelay);
0422 end
0423 end
0424
0425
0426
0427 GND.indiv_fnames{sub}=infiles{sub};
0428 GND.indiv_subnames{sub}=erpio('get_hdrvar',fid,'subdesc',1);
0429
0430
0431
0432 n_use_bins=length(use_bins);
0433 if isempty(GND.indiv_erps),
0434 GND.grands=zeros(n_use_chans,n_pts,n_use_bins)*NaN;
0435 GND.grands_stder=GND.grands;
0436 GND.grands_t=GND.grands;
0437 GND.sub_ct=zeros(1,n_use_bins);
0438 GND.indiv_bin_ct=zeros(n_subs,n_use_bins);
0439 GND.indiv_bin_raw_ct=zeros(n_subs,n_use_bins);
0440 GND.indiv_erps=zeros(n_use_chans,n_pts,n_use_bins,n_subs)*NaN;
0441 GND.indiv_art_ics=cell(1,n_subs);
0442 GND.cals.indiv_cals=zeros(n_use_chans,n_pts,n_subs)*NaN;
0443 GND.cals.indiv_cal_ct=zeros(1,n_subs);
0444 GND.cals.grand_cals=zeros(n_use_chans,n_pts)*NaN;
0445 end
0446
0447
0448
0449 pp10uv=erpio('get_hdrvar',fid,'pp10uv',1);
0450 bin_ct=0;
0451 for b=use_bins,
0452 bin_ct=bin_ct+1;
0453 GND.indiv_bin_ct(sub,bin_ct)=erpio('get_hdrvar',fid,'sums',b);
0454 GND.indiv_bin_raw_ct(sub,bin_ct)=erpio('get_hdrvar',fid,'totrawrecs',b);
0455 erps=10*erpio('readbin',fid,b)/pp10uv;
0456 erps=rmbase(erps(find(use_chans),:),n_pts,bsln_pts);
0457 GND.indiv_erps(:,:,bin_ct,sub)=erps;
0458 end
0459
0460
0461 GND.cals.indiv_cal_ct(sub)=erpio('get_hdrvar',fid,'totrawrecs',0);
0462 erps=10*erpio('readbin',fid,0)/pp10uv;
0463 erps=rmbase(erps(find(use_chans),:),n_pts,bsln_pts);
0464 GND.cals.indiv_cals(:,:,sub)=erps;
0465
0466 erpio('close',fid);
0467
0468
0469
0470 end
0471
0472
0473
0474 for b=1:n_use_bins,
0475 bin_subs=find(GND.indiv_bin_ct(:,b));
0476 GND.sub_ct(b)=length(bin_subs);
0477 if GND.sub_ct(b),
0478 GND.grands(:,:,b)=mean(GND.indiv_erps(:,:,b,bin_subs),4);
0479 GND.grands_stder(:,:,b)=std(GND.indiv_erps(:,:,b,bin_subs),0,4)/sqrt(GND.sub_ct(b));
0480 GND.grands_t(:,:,b)=GND.grands(:,:,b)./GND.grands_stder(:,:,b);
0481 else
0482 watchit(sprintf('No average files contribute to bin %d.',b));
0483 end
0484 end
0485
0486
0487 GND.cals.grand_cals=mean(GND.cals.indiv_cals,3);
0488
0489
0490 for b=1:n_use_bins,
0491 if (GND.bindesc{b}(end)==13)
0492 GND.bindesc{b}=GND.bindesc{b}(1:length(GND.bindesc{b})-1);
0493 end
0494 end
0495
0496
0497 for b=1:length(GND.condesc),
0498 if (GND.condesc{b}(end)==13)
0499 GND.condesc{b}=GND.condesc{b}(1:length(GND.condesc{b})-1);
0500 end
0501 end
0502
0503