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
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 function specGND=sets2specGND(gui_infiles_or_tmplt,varargin)
0179
0180
0181 p=inputParser;
0182 p.addRequired('gui_infiles_or_tmplt',@(x) ischar(x) || iscell(x));
0183 p.addParamValue('sub_ids',[],@isnumeric);
0184 p.addParamValue('exp_name','An Experiment',@ischar);
0185 p.addParamValue('use_bins',[],@isnumeric);
0186 p.addParamValue('exclude_chans',[],@(x) ischar(x) || iscell(x));
0187 p.addParamValue('include_chans',[],@(x) ischar(x) || iscell(x));
0188 p.addParamValue('out_fname',[],@ischar);
0189 p.addParamValue('rm_mean','yes',@ischar);
0190 p.addParamValue('n_tapers',[],@(x) isnumeric(x) && (length(x)==1));
0191 p.addParamValue('half_bandwidth',[],@(x) isnumeric(x) && (length(x)==1));
0192 p.addParamValue('pad',0,@(x) isnumeric(x) && (length(x)==1));
0193 p.addParamValue('time_wind',[],@(x) isnumeric(x) && (length(x)==2));
0194 p.addParamValue('verblevel',[],@(x) isnumeric(x) && (length(x)==1));
0195
0196 p.parse(gui_infiles_or_tmplt,varargin{:});
0197
0198
0199 global EEG;
0200 global VERBLEVEL;
0201
0202 if isempty(p.Results.verblevel),
0203 if isempty(VERBLEVEL),
0204 VERBLEVEL=2;
0205 end
0206 else
0207 VERBLEVEL=p.Results.verblevel;
0208 end
0209
0210
0211
0212
0213 if ~isempty(p.Results.include_chans) && ~isempty(p.Results.exclude_chans)
0214 error('You cannot use BOTH ''include_chans'' and ''exclude_chans'' options.');
0215 end
0216 if ischar(p.Results.exclude_chans),
0217 exclude_chans{1}=p.Results.exclude_chans;
0218 elseif isempty(p.Results.exclude_chans)
0219 exclude_chans=[];
0220 else
0221 exclude_chans=p.Results.exclude_chans;
0222 end
0223 if ischar(p.Results.include_chans),
0224 include_chans{1}=p.Results.include_chans;
0225 elseif isempty(p.Results.include_chans)
0226 include_chans=[];
0227 else
0228 include_chans=p.Results.include_chans;
0229 end
0230
0231
0232
0233 if strcmpi(gui_infiles_or_tmplt,'GUI'),
0234 loading=1;
0235 infiles=[];
0236 while loading,
0237 [neofname, inpath]=uigetfile({'*.set','*.set files'; ...
0238 '*.*','All Files (*.*)'},'EEGLAB set Files to Import','MultiSelect','on');
0239 if ischar(neofname),
0240 clear infname;
0241 infname{1}=neofname;
0242 else
0243 infname=neofname;
0244 end
0245 if ~inpath,
0246 if isempty(infiles),
0247 fprintf('File selection cancelled. Aborting sets2specGND.\n');
0248 specGND=[];
0249 return;
0250 else
0251 loading=0;
0252 end
0253 else
0254 if isempty(infiles),
0255 infiles=cell(1,length(infname));
0256 for a=1:length(infname),
0257 infiles{a}=[inpath infname{a}];
0258 end
0259 else
0260 n_files=length(infiles);
0261 ct=0;
0262 for a=(n_files+1):(n_files+length(infname)),
0263 ct=ct+1;
0264 infiles{a}=[inpath infname{ct}];
0265 end
0266 end
0267
0268 resp=questdlg('Do you want to load more files?',...
0269 'OUTPUT','Yes','No','Yes');
0270 if strcmpi(resp,'No'),
0271 loading=0;
0272 end
0273 end
0274 end
0275 elseif iscell(p.Results.gui_infiles_or_tmplt)
0276 infiles=p.Results.gui_infiles_or_tmplt;
0277 else
0278 if isempty(p.Results.sub_ids)
0279 error('If gui_infiles_or_tmplt is a string filename template, you must specify the subject numbers with the argument ''sub_ids''.');
0280 elseif ~ismember('#',p.Results.gui_infiles_or_tmplt),
0281 error('The filename template %s needs to contain a # to indicate where the subject numbers go (e.g., odbl#.nrm).', ...
0282 p.Results.gui_infiles_or_tmplt);
0283 else
0284 lb_id=find(p.Results.gui_infiles_or_tmplt=='#');
0285 prefix=p.Results.gui_infiles_or_tmplt(1:(lb_id(1)-1));
0286 postfix=p.Results.gui_infiles_or_tmplt((lb_id(1)+1):end);
0287 n_infiles=length(p.Results.sub_ids);
0288 infiles=cell(1,n_infiles);
0289 for s=1:n_infiles,
0290 no_pad=[prefix num2str(p.Results.sub_ids(s)) postfix];
0291 if p.Results.sub_ids(s)<10,
0292 padded=[prefix num2str(p.Results.sub_ids(s),'%.2d') postfix];
0293 [sP wP]=unix(['ls ' padded]);
0294 [sNP wNP]=unix(['ls ' no_pad]);
0295 if ~sP
0296 if ~sNP,
0297 error('You have a file named %s and one named %s. I don''t know which one to load!\n', ...
0298 padded,no_pad);
0299 else
0300 infiles{s}=padded;
0301 end
0302 else
0303 infiles{s}=no_pad;
0304 end
0305 else
0306 infiles{s}=no_pad;
0307 end
0308 end
0309 end
0310 end
0311
0312
0313 infiles=unique(infiles);
0314 n_infiles=length(infiles);
0315
0316
0317
0318 EEG=pop_loadset(infiles{1});
0319 EEG_var_check(EEG,infiles{1});
0320 EEG=epoch_if_necessary(EEG,p);
0321 [n_EEG_chans, n_tpts, n_epochs]=size(EEG.data);
0322
0323
0324 if ~isempty(exclude_chans),
0325 use_chans=zeros(1,n_EEG_chans);
0326 ex_ct=0;
0327 for a=1:n_EEG_chans,
0328 if ~ismember_ci(EEG.chanlocs(a).labels,exclude_chans)
0329 use_chans(a)=1;
0330 else
0331 ex_ct=ex_ct+1;
0332 ex_labels{ex_ct}=EEG.chanlocs(a).labels;
0333 end
0334 end
0335 n_chans=sum(use_chans);
0336 use_chans=find(use_chans==1);
0337
0338 if VERBLEVEL>1,
0339 missed=setdiff_ci(exclude_chans,ex_labels);
0340 n_missed=length(missed);
0341 if n_missed,
0342 if n_missed==1,
0343 msg=sprintf('I attempted to exclude the following channel, but it was not found:');
0344 else
0345 msg=sprintf('I attempted to exclude the following channels, but they were not found:');
0346 end
0347 for a=1:n_missed,
0348 msg=[msg ' ' missed{a}];
0349 end
0350 watchit(msg);
0351 end
0352 end
0353 elseif ~isempty(include_chans),
0354 use_chans=zeros(1,n_EEG_chans);
0355 in_ct=0;
0356 for a=1:n_EEG_chans,
0357 if ismember_ci(EEG.chanlocs(a).labels,include_chans)
0358 use_chans(a)=1;
0359 in_ct=in_ct+1;
0360 in_labels{in_ct}=EEG.chanlocs(a).labels;
0361 end
0362 end
0363 n_chans=sum(use_chans);
0364 use_chans=find(use_chans==1);
0365
0366 if VERBLEVEL>1,
0367 missed=setdiff_ci(include_chans,in_labels);
0368 n_missed=length(missed);
0369 if n_missed,
0370 if n_missed==1,
0371 msg=sprintf('I attempted to include the following channel, but it was not found:');
0372 else
0373 msg=sprintf('I attempted to include the following channels, but they were not found:');
0374 end
0375 for a=1:n_missed,
0376 msg=[msg ' ' missed{a}];
0377 end
0378 watchit(msg);
0379 end
0380 end
0381 else
0382 n_chans=n_EEG_chans;
0383 use_chans=1:n_chans;
0384 end
0385
0386 n_EEG_bins=length(EEG.bindesc);
0387 if isempty(p.Results.use_bins),
0388 use_bins=1:n_EEG_bins;
0389 n_bins=n_EEG_bins;
0390 else
0391
0392 if min(p.Results.use_bins)<1,
0393 error('You cannot request to import bins with indices less than 1 (e.g., "Bin -1").');
0394 elseif max(p.Results.use_bins)>n_EEG_bins,
0395 error('File %s only has %d bins, but you''ve requested to import Bin %d.\n', ...
0396 infiles{1},n_EEG_bins,max(p.Results.use_bins));
0397 end
0398 use_bins=unique(p.Results.use_bins);
0399 n_bins=length(use_bins);
0400 end
0401
0402 used_urevents=cell(n_infiles,n_bins);
0403
0404 specGND.exp_desc=p.Results.exp_name;
0405 specGND.filename=[];
0406 specGND.filepath=[];
0407 specGND.saved='no';
0408 if isempty(p.Results.time_wind),
0409 use_tpts=1:n_tpts;
0410 else
0411 tpt1=find_tpt(p.Results.time_wind(1),EEG.times);
0412 tpt2=find_tpt(p.Results.time_wind(2),EEG.times);
0413 use_tpts=tpt1:tpt2;
0414 if length(use_tpts)<2,
0415 error('Your time window is too small! It only contains one time point.');
0416 end
0417 end
0418 n_use_tpts=length(use_tpts);
0419 if VERBLEVEL>1,
0420 fprintf('Using all time points between %d and %d ms (i.e., from time point %d to %d).\n', ...
0421 EEG.times(use_tpts(1)),EEG.times(use_tpts(end)),use_tpts(1),use_tpts(end));
0422 end
0423
0424
0425
0426 T=n_use_tpts/EEG.srate;
0427 if ~isempty(p.Results.n_tapers),
0428 K=p.Results.n_tapers;
0429 if K>=(T*EEG.srate)-1,
0430 error('Too many tapers. The maximum number of possible tapers for this size time window and sampling rate is: %d', ...
0431 (T*EEG.srate)-2);
0432 end
0433 delta_f=(K+1)/(2*T);
0434 if ~isempty(p.Results.half_bandwidth) && VERBLEVEL,
0435 watchit('''bandwidth'' and ''n_tapers'' arguments to sets2specGND.m both used. Only one is needed and I will use ''n_tapers''.');
0436 end
0437 if VERBLEVEL>1
0438 fprintf('Frequency resolution half-bandwidth is: %f Hz\n',delta_f);
0439 end
0440 elseif ~isempty(p.Results.half_bandwidth),
0441 delta_f=p.Results.half_bandwidth;
0442 K=2*delta_f*T-1;
0443 K=round(delta_f*2*T-1);
0444 if K<1,
0445 error('Your frequency resolution half-bandwidth is too small. The minimum for this sized time window is: %g.', ...
0446 1/T);
0447 else
0448 delta_f=(K+1)/(2*T);
0449 fprintf('Desired half bandwidth (%f Hz) will be approximated by %f Hz.\n', ...
0450 p.Results.half_bandwidth,delta_f);
0451 end
0452 if K>=(T*EEG.srate)-1,
0453 error('Too many tapers. The maximum number of possible tapers for this size time window and sampling rate is: %d.\nUse a smaller half bandwidth.', ...
0454 (T*EEG.srate)-2);
0455 end
0456 else
0457 K=1;
0458 delta_f=(K+1)/(2*T);
0459 if VERBLEVEL>1
0460 fprintf('Frequency resolution half-bandwidth is: %f Hz\n',delta_f);
0461 end
0462 end
0463 TW=T*delta_f;
0464 if K<=2,
0465 DropLastTaper=false;
0466 else
0467 DropLastTaper=true;
0468 end
0469
0470
0471
0472
0473 if VERBLEVEL>1,
0474 fprintf('Number of tapers=%d\n',K);
0475 end
0476
0477
0478
0479 n_dft_tpts=max(2^(nextpow2(n_use_tpts)+p.Results.pad),n_use_tpts);
0480 dft_T=n_dft_tpts/EEG.srate;
0481 freq_intrvl=1/dft_T;
0482 specGND.freqs=0:freq_intrvl:(EEG.srate/2);
0483 n_freqs=length(specGND.freqs);
0484 specGND.grands_pow_dB=zeros(n_chans,n_freqs,n_bins)*NaN;
0485 specGND.grands_pow_dB_stder=specGND.grands_pow_dB;
0486 specGND.grands_pow_dB_t=specGND.grands_pow_dB;
0487 specGND.dif=zeros(1,n_bins);
0488 specGND.sub_ct=zeros(1,n_bins);
0489 specGND.chanlocs=EEG.chanlocs(use_chans);
0490 for b=1:n_bins,
0491
0492 specGND.bindesc{b}=EEG.bindesc{use_bins(b)};
0493 end
0494 specGND.srate=EEG.srate;
0495 specGND.time_pts=EEG.times(use_tpts);
0496 orig_file_times=EEG.times;
0497 specGND.n_tapers=K;
0498 specGND.half_bandwidth=delta_f;
0499 specGND.indiv_fnames=infiles;
0500 if isempty(EEG.subject),
0501 if VERBLEVEL,
0502 fprintf('Set file, %s, does not have a subject name. I will use the filename as the subject''s name.\n', ...
0503 infiles{1});
0504 end
0505 specGND.indiv_subnames{1}=infiles{1};
0506 else
0507 specGND.indiv_subnames{1}=EEG.subject;
0508 end
0509 specGND.indiv_traits=[];
0510 specGND.indiv_bin_ct=zeros(n_infiles,n_bins);
0511 if isfield(EEG,'rawtrials_per_bin'),
0512 specGND.indiv_bin_raw_ct=zeros(n_infiles,n_bins);
0513 else
0514 specGND.indiv_bin_raw_ct=zeros(n_infiles,n_bins)*NaN;
0515 end
0516 specGND.indiv_pow_dB=zeros(n_chans,n_freqs,n_bins,n_infiles);
0517 specGND.indiv_art_ics=[];
0518 if isfield(EEG,'cal_info'),
0519
0520 n_cal_tpts=size(EEG.cal_info.erps,2);
0521 specGND.cals.indiv_cals=zeros(n_chans,n_cal_tpts);
0522 specGND.cals.indiv_cal_ct=zeros(1,n_infiles);
0523 specGND.cals.grand_cals=zeros(n_chans,n_cal_tpts);
0524 cal_info_present=1;
0525 else
0526 cal_info_present=0;
0527 specGND.cals=[];
0528 end
0529 specGND.history=[];
0530 specGND.t_tests=[];
0531
0532
0533 sub_ct=1;
0534 for filenum=1:n_infiles,
0535 new_sub=1;
0536 if filenum>1,
0537 global EEG;
0538 if VERBLEVEL,
0539 fprintf('\n\n');
0540 end
0541 EEG=pop_loadset(infiles{filenum});
0542 EEG_var_check(EEG,infiles{filenum});
0543 EEG=epoch_if_necessary(EEG,p);
0544
0545
0546 if isempty(EEG.subject),
0547 if VERBLEVEL,
0548 fprintf('Set file, %s, does not have a subject name. I will use the filename as the subject''s name.\n', ...
0549 infiles{filenum});
0550 end
0551 sub_name=infiles{filenum};
0552 else
0553 sub_name=EEG.subject;
0554 end
0555 ur_sub_id=0;
0556 for old_sub=1:sub_ct,
0557 if strcmpi(specGND.indiv_subnames{old_sub},sub_name),
0558 ur_sub_id=old_sub;
0559 if VERBLEVEL
0560 fprintf('Set file, %s, will be appended to data already loaded from Subject %s.\n', ...
0561 infiles{filenum},specGND.indiv_subnames{old_sub});
0562 end
0563 new_sub=0;
0564 break;
0565 end
0566 end
0567 if ~ur_sub_id,
0568 sub_ct=sub_ct+1;
0569 sub=sub_ct;
0570 specGND.indiv_subnames{sub}=sub_name;
0571 else
0572 sub=ur_sub_id;
0573 end
0574
0575 EEG_var_check(EEG,infiles{filenum});
0576
0577 [n_EEG_chans2, n_tpts2, n_epochs]=size(EEG.data);
0578 n_EEG_bins2=length(EEG.bindesc);
0579
0580
0581 if ~isempty(exclude_chans),
0582 use_chans=zeros(1,n_EEG_chans2);
0583 ex_ct=0;
0584 ex_labels=[];
0585 for a=1:n_EEG_chans2,
0586 if ~ismember_ci(EEG.chanlocs(a).labels,exclude_chans)
0587 use_chans(a)=1;
0588 else
0589 ex_ct=ex_ct+1;
0590 ex_labels{ex_ct}=EEG.chanlocs(a).labels;
0591 end
0592 end
0593 n_chans2=sum(use_chans);
0594 use_chans=find(use_chans==1);
0595
0596 if VERBLEVEL>1,
0597 missed=setdiff_ci(exclude_chans,ex_labels);
0598 n_missed=length(missed);
0599 if n_missed,
0600 if n_missed==1,
0601 msg=sprintf('I attempted to exclude the following channel, but it was not found:');
0602 else
0603 msg=sprintf('I attempted to exclude the following channels, but they were not found:');
0604 end
0605 for a=1:n_missed,
0606 msg=[msg ' ' missed{a}];
0607 end
0608 watchit(msg);
0609 end
0610 end
0611 elseif ~isempty(include_chans),
0612 use_chans=zeros(1,n_EEG_chans2);
0613 in_ct=0;
0614 in_labels=[];
0615 for a=1:n_EEG_chans2,
0616 if ismember_ci(EEG.chanlocs(a).labels,include_chans)
0617 use_chans(a)=1;
0618 in_ct=in_ct+1;
0619 in_labels{in_ct}=EEG.chanlocs(a).labels;
0620 end
0621 end
0622 n_chans2=sum(use_chans);
0623 use_chans=find(use_chans==1);
0624
0625 if VERBLEVEL>1,
0626 missed=setdiff_ci(include_chans,in_labels);
0627 n_missed=length(missed);
0628 if n_missed,
0629 if n_missed==1,
0630 msg=sprintf('I attempted to include the following channel, but it was not found:');
0631 else
0632 msg=sprintf('I attempted to include the following channels, but they were not found:');
0633 end
0634 for a=1:n_missed,
0635 msg=[msg ' ' missed{a}];
0636 end
0637 watchit(msg);
0638 end
0639 end
0640 else
0641 n_chans2=n_EEG_chans2;
0642 use_chans=1:n_chans2;
0643 end
0644
0645
0646 if EEG.srate~=specGND.srate,
0647 error('File %s has a different sampling rate than file %s.\n', ...
0648 infiles{1},infiles{filenum});
0649 end
0650 if n_chans2~=n_chans,
0651 error('File %s has a different number of channels to import than file %s.\n', ...
0652 infiles{1},infiles{filenum});
0653 end
0654 if n_tpts2~=n_tpts,
0655 error('File %s has a different number of time points than file %s.\n', ...
0656 infiles{1},infiles{filenum});
0657 end
0658 if ~min(EEG.times==orig_file_times),
0659 error('The epochs in file %s begin and end at different times than file %s.\n', ...
0660 infiles{1},infiles{filenum});
0661 end
0662 if isempty(p.Results.use_bins),
0663 if n_EEG_bins2~=n_bins,
0664 error('File %s has a different number of bins than file %s.\n', ...
0665 infiles{1},infiles{filenum});
0666 end
0667 else
0668 if max(p.Results.use_bins)>n_EEG_bins2,
0669 error('File %s only has %d bins, but you''ve requested to import Bin %d.\n', ...
0670 infiles{filenum},n_EEG_bins2,max(p.Results.use_bins));
0671 end
0672 end
0673
0674 bin_ct=0;
0675 for b=use_bins,
0676 bin_ct=bin_ct+1;
0677 if ~strcmpi(specGND.bindesc{bin_ct},EEG.bindesc{b}),
0678 error('The #%d imported bin in file %s is different than that of file %s.\n', ...
0679 b,infiles{1},infiles{filenum});
0680 end
0681 end
0682 try
0683 [fs1, fs2, er]=comp_struct_quiet(specGND.chanlocs,EEG.chanlocs(use_chans));
0684 catch
0685 error('File %s''s imported channel location information differs from that of file %s.\n', ...
0686 infiles{1},infiles{filenum});
0687 end
0688 if ~isempty(er),
0689 error('File %s''s imported channel location information differs from that of file %s.\n', ...
0690 infiles{1},infiles{filenum});
0691 end
0692 else
0693 sub=sub_ct;
0694 end
0695
0696
0697 fldnames=fieldnames(EEG);
0698 art_ics=[];
0699 ics_removed=0;
0700 if sum(EEG.reject.gcompreject),
0701 ics_removed=1;
0702 else
0703 for fn=1:length(fldnames),
0704 if strcmpi(fldnames{fn},'iclabels'),
0705 ics_removed=1;
0706 break;
0707 end
0708 end
0709 end
0710 if ics_removed,
0711 if strcmpi(p.Results.rm_mean,'yes'),
0712 art_ics=remove_artifact_ics([EEG.times(use_tpts(1)) EEG.times(use_tpts(end))]);
0713 else
0714 art_ics=remove_artifact_ics([]);
0715 end
0716 end
0717 specGND.indiv_art_ics{filenum}=art_ics;
0718
0719
0720 if strcmpi(p.Results.rm_mean,'yes'),
0721 if ~ics_removed,
0722 if VERBLEVEL>=2,
0723 fprintf('Baselining data by removing mean of selected time window (%d to %d).\n', ...
0724 round(EEG.times(use_tpts(1))),round(EEG.times(use_tpts(end))));
0725 end
0726
0727
0728 EEG.data=rmbase(EEG.data,n_tpts,use_tpts);
0729 EEG.data=reshape(EEG.data,n_EEG_chans,n_tpts,n_epochs);
0730 end
0731 end
0732
0733 epoch_in_bin=zeros(n_bins,n_epochs);
0734
0735 for a=1:n_epochs,
0736 for b=1:length(EEG.epoch(a).eventtype),
0737 if (length(EEG.epoch(a).eventtype{b})>2) && strcmpi(EEG.epoch(a).eventtype{b}(1:3),'bin')
0738 bin=str2num(EEG.epoch(a).eventtype{b}(4:end));
0739 if ismember(bin,use_bins) && ~index_cellarray_or_vector(EEG.epoch(a).eventlatency,b,VERBLEVEL),
0740
0741
0742 binind=find(use_bins==bin);
0743
0744 new_event=1;
0745 urevent=index_cellarray_or_vector(EEG.epoch(a).eventurevent,b,VERBLEVEL);
0746 if ~new_sub,
0747
0748
0749 if ismember(urevent,used_urevents{sub,binind}),
0750 if VERBLEVEL>1,
0751 fprintf('Epoch %d has already been added to Bin %d for Subject %s.\n', ...
0752 a,bin,specGND.indiv_subnames{sub});
0753 end
0754 new_event=0;
0755 end
0756 end
0757 if new_event,
0758 epoch_in_bin(binind,a)=1;
0759 used_urevents{sub,binind}=[used_urevents{sub,binind} urevent];
0760 end
0761 end
0762 end
0763 end
0764 end
0765
0766 for b=use_bins,
0767 binind=find(use_bins==b);
0768 bin_ids=find(epoch_in_bin(binind,:));
0769 specGND.indiv_bin_ct(sub,binind)=specGND.indiv_bin_ct(sub,binind)+length(bin_ids);
0770 chan_ct=0;
0771 for c=use_chans,
0772 chan_ct=chan_ct+1;
0773
0774
0775
0776
0777
0778
0779
0780 ep_ct=0;
0781 S=zeros(n_freqs,length(bin_ids));
0782 for ep=bin_ids,
0783 ep_ct=ep_ct+1;
0784 [S(:,ep_ct), f]=pmtm(squeeze(EEG.data(c,use_tpts,bin_ids(ep_ct))),TW,n_dft_tpts,EEG.srate,'DropLastTaper',DropLastTaper,'unity');
0785 end
0786
0787
0788
0789
0790
0791 specGND.indiv_pow_dB(chan_ct,:,binind,sub)=specGND.indiv_pow_dB(chan_ct,:,binind,sub)+sum(10*log10(S),2)';
0792 end
0793 if (binind==1) && (chan_ct==1),
0794 specGND.freqs=f;
0795 end
0796
0797
0798
0799 end
0800
0801 if isfield(EEG,'rawtrials_per_bin'),
0802
0803
0804 specGND.indiv_bin_raw_ct(sub,:)=specGND.indiv_bin_raw_ct(sub,:)+EEG.rawtrials_per_bin(use_bins);
0805 end
0806
0807 if isfield(EEG,'cal_info')
0808
0809
0810
0811
0812 specGND.cals.indiv_cals(:,:,sub)=EEG.cal_info.erps(use_chans,:);
0813 specGND.cals.indiv_cal_ct(sub)=EEG.cal_info.npulse_used;
0814 specGND.cals.caldesc='cal pulses';
0815 cal_info_present=1;
0816 end
0817
0818 clear global EEG;
0819 end
0820
0821
0822
0823
0824 specGND.indiv_bin_ct=specGND.indiv_bin_ct(1:sub_ct,:);
0825 specGND.indiv_bin_raw_ct=specGND.indiv_bin_raw_ct(1:sub_ct,:);
0826 specGND.indiv_pow_dB=specGND.indiv_pow_dB(:,:,:,1:sub_ct);
0827 if cal_info_present,
0828 specGND.cals.indiv_cals(:,:,1:sub_ct)=specGND.cals.indiv_cals(:,:,1:sub_ct);
0829 specGND.cals.indiv_cal_ct=specGND.cals.indiv_cal_ct(1:sub_ct);
0830 end
0831
0832
0833 for sub=1:sub_ct,
0834 if VERBLEVEL>1,
0835 fprintf('\nTrials per bin for Subject %s:\n',specGND.indiv_subnames{sub});
0836 end
0837 for b=1:n_bins,
0838 bin_ct=specGND.indiv_bin_ct(sub,b);
0839 if bin_ct,
0840 if VERBLEVEL>1,
0841 fprintf('Bin %d (%s): %d trials\n',b,specGND.bindesc{b},bin_ct);
0842 end
0843 specGND.indiv_pow_dB(:,:,b,sub)=specGND.indiv_pow_dB(:,:,b,sub)/bin_ct;
0844 specGND.sub_ct(b)=specGND.sub_ct(b)+1;
0845 else
0846 watchit(sprintf('Subject %s has no epochs that fall into bin %d.',specGND.indiv_subnames{sub},b));
0847 specGND.indiv_pow_dB(:,:,b,sub)=specGND.indiv_pow_dB(:,:,b,sub)*NaN;
0848 end
0849 end
0850 end
0851
0852
0853 if VERBLEVEL>1,
0854 fprintf('\n\n');
0855 end
0856
0857
0858 for b=1:n_bins,
0859 bin_subs=find(specGND.indiv_bin_ct(:,b));
0860 if specGND.sub_ct(b),
0861 specGND.grands_pow_dB(:,:,b)=mean(specGND.indiv_pow_dB(:,:,b,bin_subs),4);
0862 specGND.grands_pow_dB_stder(:,:,b)=std(specGND.indiv_pow_dB(:,:,b,bin_subs),0,4)/sqrt(specGND.sub_ct(b));
0863 specGND.grands_pow_dB_t(:,:,b)=specGND.grands_pow_dB(:,:,b)./specGND.grands_pow_dB_stder(:,:,b);
0864 else
0865 watchit(sprintf('No average files contribute to bin %d.',b));
0866 end
0867 end
0868
0869
0870 if cal_info_present,
0871 specGND.cals.grand_cals=mean(specGND.cals.indiv_cals,3);
0872 end
0873
0874
0875 if isempty(p.Results.out_fname),
0876
0877 [jname, jpath]=uiputfile({'*.specGND','*.specGND files'; ...
0878 '*','All files'},'Save specGND variable as:','untitled.specGND');
0879 if ~jpath,
0880 fprintf('Output filename selection cancelled. specGND variable NOT saved to disk.\n');
0881 else
0882
0883 isW=isWriteable([jpath jname]);
0884 if isW,
0885 specGND=save_matmk(specGND,jname,jpath,1);
0886 else
0887 fprintf('specGND file could not be saved, but should still exist in MATLAB workspace.');
0888 end
0889 end
0890 elseif ~strcmpi(p.Results.out_fname,'no save'),
0891 [jpath, jname]=pathNname(p.Results.out_fname);
0892
0893 if ~ismember('.',jname),
0894 jname=[jname '.specGND'];
0895 end
0896 specGND=save_matmk(specGND,jname,jpath);
0897 end
0898
0899
0900
0901
0902 function EEG_var_check(EEG,fname)
0903
0904 if ~isfield(EEG,'bindesc'),
0905 error('The variable stored in file %s does not contain a bin descriptor. You need to apply bin_info2EEG.m to it.',fname);
0906 end
0907
0908 function value=index_cellarray_or_vector(cellarray_or_vector,index,VERBLEVEL)
0909
0910 if index>length(cellarray_or_vector),
0911
0912
0913
0914 index=length(cellarray_or_vector);
0915 if VERBLEVEL>2,
0916 fprintf('EEG.epoch(#).eventtype index exceeds EEG.epoch(#).latency index. I will simply use the last element of EEG.epoch(#).latency.\n');
0917 end
0918 end
0919
0920 if iscell(cellarray_or_vector),
0921 value=cellarray_or_vector{index};
0922 else
0923 value=cellarray_or_vector(index);
0924 end
0925
0926
0927 function EEG=epoch_if_necessary(EEG,p)
0928
0929
0930
0931
0932
0933
0934
0935 if length(size(EEG.data))==2
0936
0937 time_wind=p.Results.time_wind/1000;
0938 if isempty(time_wind),
0939 time_wind=[-.1 1.5];
0940 elseif time_wind(1)>=0,
0941 time_wind(1)=-2/EEG.srate;
0942 end
0943 use_bins=p.Results.use_bins;
0944 if isempty(use_bins),
0945
0946 n_ev=length(EEG.event);
0947 for a=1:n_ev,
0948 if length(EEG.event(a).type)>3,
0949 if strcmpi(EEG.event(a).type(1:3),'bin'),
0950 ev_bin=str2double(EEG.event(a).type(4:end));
0951 if ~ismember(ev_bin,use_bins)
0952 use_bins=[use_bins ev_bin];
0953 end
0954 end
0955 end
0956 end
0957 end
0958 n_use_bins=length(use_bins);
0959 if ~n_use_bins,
0960 error('This set file does not contain any events of type ''bin#''. You need to run bin_info2EEG.m on it.');
0961 end
0962 use_types=cell(1,n_use_bins);
0963 for a=1:n_use_bins,
0964 use_types{a}=['bin' int2str(use_bins(a))];
0965 end
0966 EEG=pop_epoch(EEG,use_types,time_wind, 'newname',[EEG.setname '_epoched'], 'epochinfo', 'yes');
0967 end
0968
0969
0970 function isW = isWriteable(inFile)
0971
0972
0973
0974
0975
0976
0977 isW = 0;
0978 [fid, message] = fopen(inFile,'a+');
0979 if (fid ~= -1)
0980 isW = 1;
0981 fclose(fid);
0982 else
0983 fprintf('Error opening %s: %s\n', inFile, message);
0984 end
0985
0986
0987
0988 function yesno=ismember_ci(str,str_array)
0989
0990
0991
0992
0993
0994
0995
0996
0997
0998
0999
1000 yesno=0;
1001 n_str=length(str_array);
1002
1003 for a=1:n_str,
1004 if strcmpi(str,str_array{a})
1005 yesno=1;
1006 break;
1007 end
1008 end
1009
1010
1011
1012 function dif_str=setdiff_ci(superset,subset)
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024 n_super=length(superset);
1025 n_sub=length(subset);
1026 dif_ct=0;
1027 dif_str=[];
1028 for a=1:n_super,
1029 found=0;
1030 for b=1:n_sub,
1031 if strcmpi(superset{a},subset{b}),
1032 found=1;
1033 break
1034 end
1035 end
1036 if ~found,
1037 dif_ct=dif_ct+1;
1038 dif_str{dif_ct}=superset{a};
1039 end
1040 end