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 function specGRP=specGNDs2specGRP(gui_infiles_or_tmplt,varargin)
0111
0112 p=inputParser;
0113 p.addRequired('gui_infiles_or_tmplt',@(x) ischar(x) || iscell(x));
0114 p.addParamValue('specGND_ids',[],@isnumeric);
0115 p.addParamValue('use_bins',[],@isnumeric);
0116 p.addParamValue('exclude_chans',[],@(x) ischar(x) || iscell(x));
0117 p.addParamValue('include_chans',[],@(x) ischar(x) || iscell(x));
0118 p.addParamValue('out_fname',[],@ischar);
0119 p.addParamValue('exp_desc',[],@ischar);
0120 p.addParamValue('group_desc',[],@iscell);
0121 p.addParamValue('create_difs','no',@(x) ischar(x) && (strcmpi(x,'no') || strcmpi(x,'yes')) );
0122 p.parse(gui_infiles_or_tmplt,varargin{:});
0123
0124
0125
0126
0127 if ~isempty(p.Results.include_chans) && ~isempty(p.Results.exclude_chans)
0128 error('You cannot use BOTH ''include_chans'' and ''exclude_chans'' options.');
0129 end
0130 if ischar(p.Results.exclude_chans),
0131 exclude_chans{1}=p.Results.exclude_chans;
0132 elseif isempty(p.Results.exclude_chans)
0133 exclude_chans=[];
0134 else
0135 exclude_chans=p.Results.exclude_chans;
0136 end
0137 if ischar(p.Results.include_chans),
0138 include_chans{1}=p.Results.include_chans;
0139 elseif isempty(p.Results.include_chans)
0140 include_chans=[];
0141 else
0142 include_chans=p.Results.include_chans;
0143 end
0144
0145
0146 specGRP.exp_desc=p.Results.exp_desc;
0147 specGRP.filename=[];
0148 specGRP.filepath=[];
0149 specGRP.saved='no';
0150 specGRP.freqs=[];
0151 specGRP.group_desc=[];
0152 specGRP.specGND_fnames=[];
0153 specGRP.grands_pow_dB=[];
0154 specGRP.grands_pow_dB_stder=[];
0155 specGRP.grands_pow_dB_t=[];
0156 specGRP.dif=[];
0157 specGRP.chanlocs=[];
0158 specGRP.bin_info=[];
0159 specGRP.srate=[];
0160 specGRP.time_pts=[];
0161 specGRP.n_tapers=[];
0162 specGRP.half_bandwidth=[];
0163 specGRP.indiv_fnames=[];
0164 specGRP.indiv_subnames=[];
0165 specGRP.indiv_traits=[];
0166 specGRP.indiv_bin_ct=[];
0167 specGRP.indiv_bin_raw_ct=[];
0168 specGRP.indiv_art_ics=[];
0169 specGRP.cals.indiv_cals=[];
0170 specGRP.cals.indiv_cal_ct=[];
0171 specGRP.cals.grand_cals=[];
0172 specGRP.history=[];
0173 specGRP.t_tests=[];
0174
0175
0176 if strcmpi(gui_infiles_or_tmplt,'GUI'),
0177 loading=1;
0178 infiles=[];
0179 while loading,
0180 fprintf('Select specGND files to import.\n');
0181 fprintf('To select multiple contiguous files in the same directory, hold down the shift key when selecting.\n');
0182 fprintf('To select files in multiple directories or non-contiguous files in the same direcotry,\n');
0183 fprintf('select all the files in one directory and hit the "Open" button.\n');
0184 fprintf('You will then be given the opportunity to load more files from the current or another directory.\n');
0185 [neofname, inpath]=uigetfile({'*.specGND','*.specGND files'; ...
0186 '*.*','All Files (*.*)'},'MATLABmk specGND Files to Import','MultiSelect','on');
0187 if ischar(neofname),
0188 clear infname;
0189 infname{1}=neofname;
0190 else
0191 infname=neofname;
0192 end
0193 if ~inpath,
0194 if isempty(infiles),
0195 fprintf('File selection cancelled. Aborting specGNDs2specGRP.\n');
0196 specGRP=[];
0197 return;
0198 else
0199 loading=0;
0200 end
0201 else
0202 if isempty(infiles),
0203 infiles=cell(1,length(infname));
0204 for a=1:length(infname),
0205 infiles{a}=[inpath infname{a}];
0206 end
0207 else
0208 n_files=length(infiles);
0209 ct=0;
0210 for a=(n_files+1):(n_files+length(infname)),
0211 ct=ct+1;
0212 infiles{a}=[inpath infname{ct}];
0213 end
0214 end
0215
0216 resp=questdlg('Do you want to load more files?',...
0217 'OUTPUT','Yes','No','Yes');
0218 if strcmpi(resp,'No'),
0219 loading=0;
0220 end
0221 end
0222 end
0223 elseif iscell(p.Results.gui_infiles_or_tmplt)
0224 infiles=p.Results.gui_infiles_or_tmplt;
0225 else
0226 if isempty(p.Results.specGND_ids)
0227 error('If gui_infiles_or_tmplt is a string filename template, you must specify the specGND file numbers with the argument ''specGND_ids''.');
0228 elseif ~ismember('#',p.Results.gui_infiles_or_tmplt),
0229 error('The filename template %s needs to contain a # to indicate where the specGND file numbers go (e.g., yngvob#.specGND).', ...
0230 p.Results.gui_infiles_or_tmplt);
0231 else
0232 lb_id=find(p.Results.gui_infiles_or_tmplt=='#');
0233 prefix=p.Results.gui_infiles_or_tmplt(1:(lb_id(1)-1));
0234 postfix=p.Results.gui_infiles_or_tmplt((lb_id(1)+1):end);
0235 n_subs=length(p.Results.specGND_ids);
0236 infiles=cell(1,n_subs);
0237 for s=1:n_subs,
0238 no_pad=[prefix num2str(p.Results.specGND_ids(s)) postfix];
0239 if p.Results.specGND_ids(s)<10,
0240 padded=[prefix num2str(p.Results.specGND_ids(s),'%.2d') postfix];
0241 [sP wP]=unix(['ls ' padded]);
0242 [sNP wNP]=unix(['ls ' no_pad]);
0243 if ~sP
0244 if ~sNP,
0245 error('You have a file named %s and one named %s. I don''t know which one to load!\n', ...
0246 padded,no_pad);
0247 else
0248 infiles{s}=padded;
0249 end
0250 else
0251 infiles{s}=no_pad;
0252 end
0253 else
0254 infiles{s}=no_pad;
0255 end
0256 end
0257 end
0258 end
0259
0260
0261 if ~isempty(p.Results.group_desc),
0262 if length(p.Results.group_desc)~=length(infiles),
0263 error('The number of group descriptors needs to equal the number of specGND files.');
0264 else
0265 specGRP.group_desc=p.Results.group_desc;
0266 end
0267 end
0268
0269 if strcmpi(p.Results.create_difs,'yes'),
0270 create_difs=1;
0271 else
0272 create_difs=0;
0273 end
0274
0275 specGRP.specGND_fnames=infiles;
0276 n_groups=length(infiles);
0277 if n_groups<2,
0278 error('You need at least two specGND files to make a specGRP variable.');
0279 end
0280
0281 for grp=1:n_groups,
0282
0283
0284 specGND=[];
0285 load(infiles{grp},'-MAT');
0286 if isempty(specGND),
0287 error('File %s needs to contain a MATLABmk specGND variable that is called "specGND"',infiles{grp});
0288 end
0289
0290 if isempty(p.Results.group_desc),
0291 resp=inputdlg(sprintf('Enter the group descriptor for the participants in file %s (e.g., patients).', ...
0292 infiles{grp}),'Name Group of Participants');
0293 specGRP.group_desc{grp}=resp{1};
0294 end
0295
0296
0297 if grp==1,
0298
0299 [n_chans, n_freqs, n_bins, n_subs1]=size(specGND.indiv_pow_dB);
0300
0301 if create_difs,
0302
0303 if isempty(p.Results.use_bins),
0304 use_bins=1:n_bins;
0305 else
0306 use_bins=p.Results.use_bins;
0307 end
0308 n_use_bins=length(use_bins);
0309 end
0310
0311 if isempty(specGRP.exp_desc),
0312 specGRP.exp_desc=specGND.exp_desc;
0313 end
0314
0315
0316 if ~isempty(exclude_chans),
0317 use_chans=ones(1,n_chans);
0318 for c=1:n_chans,
0319 for a=1:length(exclude_chans)
0320 if strcmpi(specGND.chanlocs(c).labels,exclude_chans{a}),
0321 use_chans(c)=0;
0322 break;
0323 end
0324 end
0325 end
0326 use_chans=find(use_chans>0);
0327 elseif ~isempty(include_chans),
0328 use_chans=zeros(1,n_chans);
0329 for c=1:n_chans,
0330 for a=1:length(include_chans)
0331 if strcmpi(specGND.chanlocs(c).labels,include_chans{a}),
0332 use_chans(c)=1;
0333 break;
0334 end
0335 end
0336 end
0337 use_chans=find(use_chans>0);
0338 else
0339 use_chans=1:n_chans;
0340 end
0341 specGRP.chanlocs=specGND.chanlocs(use_chans);
0342 specGRP.freqs=specGND.freqs;
0343 specGRP.time_pts=specGND.time_pts;
0344 specGRP.n_tapers=specGND.n_tapers;
0345 specGRP.half_bandwidth=specGND.half_bandwidth;
0346 specGRP.srate=specGND.srate;
0347 if isempty(specGND.cals),
0348 specGRP.cals=[];
0349 else
0350 n_cal_tpts=size(specGND.cals.indiv_cals,2);
0351 specGRP.cals.grand_cals=zeros(length(use_chans),n_cal_tpts,n_groups);
0352 specGRP.cals.caldesc=specGND.cals.caldesc;
0353 end
0354
0355 if create_difs,
0356
0357 bin_sub_ct=zeros(n_use_bins,n_groups);
0358 bindesc=cell(1,n_use_bins);
0359 bin_ct=0;
0360 for b=use_bins,
0361 bin_ct=bin_ct+1;
0362 bindesc{bin_ct}=specGND.bindesc{b};
0363 end
0364 end
0365 else
0366
0367 [n_chansNEO, n_freqsNEO, n_binsNEO, n_subsNEO]=size(specGND.indiv_pow_dB);
0368
0369
0370 if ~isempty(exclude_chans),
0371 use_chans=ones(1,n_chansNEO);
0372 for c=1:n_chansNEO,
0373 for a=1:length(exclude_chans),
0374 if strcmpi(specGND.chanlocs(c).labels,exclude_chans{a}),
0375 use_chans(c)=0;
0376 break;
0377 end
0378 end
0379 end
0380 use_chans=find(use_chans>0);
0381 elseif ~isempty(include_chans),
0382 use_chans=zeros(1,n_chansNEO);
0383 for c=1:n_chansNEO,
0384 for a=1:length(include_chans)
0385 if strcmpi(specGND.chanlocs(c).labels,include_chans{a}),
0386 use_chans(c)=1;
0387 break;
0388 end
0389 end
0390 end
0391 use_chans=find(use_chans>0);
0392 else
0393 use_chans=1:n_chansNEO;
0394 end
0395
0396
0397 if ~isequal(specGND.chanlocs(use_chans),specGRP.chanlocs),
0398 error('The channel location information in the specGND variable from file %s differs from that in previous files.', ...
0399 infiles{grp});
0400 end
0401
0402 if ~isequal(specGND.freqs,specGRP.freqs),
0403 error('The frequencies in the specGND variable from file %s differ from those in previous files.', ...
0404 infiles{grp});
0405 end
0406
0407 if ~isequal(specGND.time_pts,specGRP.time_pts),
0408 error('The time points in the specGND variable from file %s differ from those in previous files.', ...
0409 infiles{grp});
0410 end
0411
0412 if ~isequal(specGND.srate,specGRP.srate),
0413 error('The sampling rate in the specGND variable from file %s differs from that in previous files.', ...
0414 infiles{grp});
0415 end
0416
0417 if ~isequal(specGND.n_tapers,specGRP.n_tapers),
0418 error('The number of tapers in the specGND variable from file %s differs from that in previous files.', ...
0419 infiles{grp});
0420 end
0421
0422 if ~isequal(specGND.half_bandwidth,specGRP.half_bandwidth),
0423 error('The half frequency bandwidth in the specGND variable from file %s differs from that in previous files.', ...
0424 infiles{grp});
0425 end
0426
0427 if ~isempty(specGND.cals) && ~strcmpi(specGND.cals.caldesc,specGRP.cals.caldesc)
0428 watchit(sprintf('The cal pulse bin descriptor in the specGND variable from file %s differs from that in previous files.', ...
0429 infiles{grp}));
0430 end
0431
0432 if create_difs,
0433 bin_ct=0;
0434 for b=use_bins,
0435 bin_ct=bin_ct+1;
0436 if ~strcmpi(bindesc{bin_ct},specGND.bindesc{b}),
0437 watchit('Bin descriptor for Bin %d in file %s, differs from that in previous file(s).', ...
0438 b,infiles{grp});
0439 fprintf('Bin descriptor for Bin %d, file %s is %s.\n',b,infiles{grp},specGND.bindesc{b})
0440 fprintf('Bin descriptor for Bin %d, previous file(s) is %s. Using bin descriptor from previous file(s).\n',b,bindesc{bin_ct});
0441 end
0442 end
0443 end
0444 end
0445 specGRP.indiv_fnames{grp}=specGND.indiv_fnames;
0446 specGRP.indiv_subnames{grp}=specGND.indiv_subnames;
0447 specGRP.indiv_traits{grp}=specGND.indiv_traits;
0448 specGRP.indiv_art_ics{grp}=specGND.indiv_art_ics;
0449 if ~isempty(specGND.cals),
0450 specGRP.cals.indiv_cals{grp}=specGND.cals.indiv_cals(use_chans,:,:);
0451 specGRP.cals.indiv_cal_ct{grp}=specGND.cals.indiv_cal_ct;
0452 specGRP.cals.grand_cals(:,:,grp)=specGND.cals.grand_cals(use_chans,:);
0453 end
0454
0455 if create_difs,
0456 bin_sub_ct(:,grp)=specGND.sub_ct(use_bins);
0457 specGRP.indiv_bin_ct{grp}=specGND.indiv_bin_ct(:,use_bins);
0458 specGRP.indiv_bin_raw_ct{grp}=specGND.indiv_bin_raw_ct(:,use_bins);
0459
0460
0461 group_grands(:,:,:,grp)=specGND.grands_pow_dB(use_chans,:,use_bins);
0462 n_subs=zeros(1,1,n_use_bins);
0463 n_subs(1,1,:)=specGND.sub_ct(use_bins);
0464 n_subs=repmat(n_subs,[length(use_chans) n_freqs 1]);
0465 group_ss(:,:,:,grp)=(specGND.grands_pow_dB_stder(use_chans,:,use_bins).^2).*n_subs.*(n_subs-1);
0466 end
0467 end
0468
0469 if create_difs,
0470
0471 dif_ct=0;
0472 for grpA=1:n_groups,
0473 for grpB=(grpA+1):n_groups,
0474 for b=1:n_use_bins,
0475 dif_ct=dif_ct+1;
0476 nA=bin_sub_ct(b,grpA);
0477 nB=bin_sub_ct(b,grpB);
0478 specGRP.grands_pow_dB(:,:,dif_ct)=group_grands(:,:,b,grpA)-group_grands(:,:,b,grpB);
0479 SS_pooled=(group_ss(:,:,b,grpA)+group_ss(:,:,b,grpB))/(nA+nB-2);
0480 specGRP.grands_pow_dB_stder(:,:,dif_ct)=sqrt( SS_pooled*(nA+nB)/(nA*nB) );
0481 specGRP.dif(dif_ct)=1;
0482
0483 specGRP.bin_info(b).bindesc=sprintf('%s (%s-%s)',bindesc{b}, ...
0484 specGRP.group_desc{grpA},specGRP.group_desc{grpB});
0485 specGRP.bin_info(b).groupA=grpA;
0486 specGRP.bin_info(b).groupB=grpB;
0487 specGRP.bin_info(b).source_binA=b;
0488 specGRP.bin_info(b).source_binB=b;
0489 specGRP.bin_info(b).n_subsA=nA;
0490 specGRP.bin_info(b).n_subsB=nB;
0491 specGRP.bin_info(b).op='A-B';
0492 end
0493 end
0494 end
0495 specGRP.grands_pow_dB_t=specGRP.grands_pow_dB./specGRP.grands_pow_dB_stder;
0496 end
0497
0498 if isempty(p.Results.out_fname),
0499
0500 [jname, jpath]=uiputfile({'*.specGRP','*.specGRP files'; ...
0501 '*.*','All files'},'Save specGRP variable as:','untitled.specGRP');
0502 if ~jpath,
0503 fprintf('Output filename selection cancelled. specGRP variable NOT saved to disk.\n');
0504 else
0505 specGRP=save_matmk(specGRP,jname,jpath,1);
0506 end
0507 elseif ~strcmpi(p.Results.out_fname,'no save'),
0508 [jpath, jname]=pathNname(p.Results.out_fname);
0509 specGRP=save_matmk(specGRP,jname,jpath);
0510 end