⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pop_chanedit.m

📁 含有多种ICA算法的eeglab工具箱
💻 M
📖 第 1 页 / 共 3 页
字号:
%						  ['[tmpf tmpp] = uigetfile(''*'', ''Load a channel location file''); drawnow;' ...
%						   'tmpfmt = inputdlg2({[''File format [loc|sph|sfp|xyz|asc|dat|polhemusx|polhemusy|besa|chanedit] ([]=use extension)'' ]}, ''File format'', 1, { '''' }, ''readlocs'');' ...
%						   'if isempty(tmpfmt), tmpfmt = {[]}; tmpp = []; tmpf = []; end;' ...
%						   'comtmp = {''load'' { [tmpp tmpf ] ''filetype'' tmpfmt{1} } }; clear tmpfmt tmpp tmpf;' endgui ] }, ...
		if guimodif
			set(currentfig, 'userdat', {});
			eval([ callpart1 callpart2 ]);
			[results userdat returnmode] = inputgui( geometry, uilist, 'pophelp(''pop_chanedit'');', 'Edit channel info -- pop_chanedit()', {}, currentfig );
		else 
			[results userdat returnmode] = inputgui( geometry, uilist, 'pophelp(''pop_chanedit'');', 'Edit channel info -- pop_chanedit()', {}, 'noclose' );
			if ~isempty(get(0, 'currentfigure')) currentfig = gcf; end;
		end; 
		if length(results) == 0, return; end;
		
		% transfer events back from global workspace
		chans = evalin('base', 'chantmp');
		%evalin('base', 'clear chantmp');
		totaluserdat = { totaluserdat{:} userdat{:}};
		if evalin('base', 'exist(''comtmp'')') == 1
			tmpcom = evalin('base', 'comtmp');
            if ~isempty(tmpcom)
                try, 
                    chans = pop_chanedit(chans, tmpcom{:}); % apply modification to channel structure
                    if iscell(tmpcom{2}) & (length(tmpcom{2}) == 2) & isstr(tmpcom{2}{2}) & strcmpi(tmpcom{2}{2}, 'gui'),
                        tmpcom = { tmpcom{1} tmpcom{2}{1} };
                    end;
                    if iscell(tmpcom{2}) & (length(tmpcom{2}) > 0) & isstr(tmpcom{2}{1}) & strcmpi(tmpcom{2}{1}, 'chancenter')
                        tmpcom = { tmpcom{1} { tmpcom{2}{1} tmpcom{2}{2} 0 } };
                    end;
                    totaluserdat = { totaluserdat{:} tmpcom{:} };
                catch
                    errordlg2(lasterr, 'Channel location error');
                    returnmode = 'no';
                end;	
                evalin('base', 'clear comtmp');
            end;
            chans = checkchans(chans, allfields);
		end;
		
		% handle arguments
		% ----------------
		if strcmp(returnmode, 'retuninginputui')
			ingui = 0;
			if nbchan ~= 0 & nbchan ~= length(chans)
				if ~popask(strvcat(['The number of channel (' int2str(length(chans)) ') does not correspond to the'], ...
								  ['initial number of channel (' int2str(nbchan) '), so the channel information'], ...
								  'may be removed if this function was called from EEGLAB'))
					ingui = 1;
				end;
			end;	
		else 
			guimodif = 1;
		end;
	end;
    
	% not in the loop, returning
	% --------------------------
	if ~isempty(findobj('parent', gcf, 'tag','shrinkfactor'))
        tmpshrinkskirt = get(findobj('parent', gcf, 'tag','shrinkbut'), 'value');
        tmpval         = num2str(get(findobj('parent', gcf, 'tag','shrinkfactor'), 'string'));
        
        if tmpshrinkskirt,
            chans(1).shrink = 'auto';
            totaluserdat{end+1} = 'shrink';
            totaluserdat{end+1} = 'auto';
        end;
        if ~isempty(tmpval)
            chans(1).plotrad  = str2num(tmpval);
            totaluserdat{end+1} = 'plotrad';
            totaluserdat{end+1} = chans(1).plotrad;
        end;
		close(gcf);
		if ~isempty( totaluserdat )
			if isempty(inputname(1)), varname = 'EEG.chanlocs';
			else varname = inputname(1);
			end;
            com = sprintf('%s=pop_chanedit(%s, %s);', varname, varname, vararg2str(totaluserdat));
		end;
	end;
	evalin('base', 'clear global chantmp; clear chantmp;');
else 
     args = varargin;
	 % no interactive inputs
	 % scan all the fields of g
	 % ------------------------
	 for curfield = 1:2:length(args)
		 switch lower(args{curfield})
          case 'forcelocs',
           if ~isempty(args{curfield+1})
               chans = forcelocs(chans,args{curfield+1});
               disp('Convert XYZ coordinates to spherical and polar');
           end;
          case 'eval',
           chantmp = chans;
           eval( args{curfield+1} );
           chans = chantmp;
		  case 'convert', 
		   if iscell(args{curfield+1})
			   method=args{curfield+1}{1};
			   extraargs = args{curfield+1}(2:end);
		   else
			   method=args{curfield+1};
			   extraargs = {''};
		   end;
		   if isstr(extraargs{1}) & strcmp(extraargs{1}, 'gui') & ~strcmp(method, 'chancenter')
			   tmpButtonName=questdlg2( strvcat('This will modify fields in the channel structure', ...
					'Are you sure you want to apply this function ?'), 'Confirmation', 'Cancel', 'Yes','Yes');
               if ~strcmpi(tmpButtonName, 'Yes'), return; end;
		   end;
		   switch method
			case 'chancenter',
			 if isempty(extraargs)
				 [X Y Z]=chancenter(cell2mat({chans.X})', cell2mat({chans.Y})', cell2mat({chans.Z})',[]);
			 else
				 [X Y Z]=chancenter(cell2mat({chans.X})', cell2mat({chans.Y})', cell2mat({chans.Z})', extraargs{:});
			 end;
			 if isempty(X), return; end;
			 for index = 1:length(chans)
				 chans(index).X  = X(index);
				 chans(index).Y  = Y(index);
				 chans(index).Z  = Z(index);
			 end;
             disp('Note: automatically convert XYZ coordinates to spherical and polar');
             chans = convertlocs(chans, 'cart2all');
            otherwise
             chans = convertlocs(chans, method, 'verbose', 'on');
		   end;
		  case 'transform'
		   try, tmpoper = args{curfield+1}; catch, return; end;
		   if isempty(deblank(tmpoper)), return; end;
		   if iscell(tmpoper), tmpoper = tmpoper{1}; end;
           tmpoper = [ tmpoper ';' ];
		   if isempty(findstr(tmpoper, 'chans'))
			   try, X          = cell2mat({chans.X});          catch, X(1:length(chans)) = NaN; end;
			   try, Y          = cell2mat({chans.Y});          catch, Y(1:length(chans)) = NaN; end;
			   try, Z          = cell2mat({chans.Z});          catch, Z(1:length(chans)) = NaN; end;
			   try, theta      = cell2mat({chans.theta});      catch, theta(1:length(chans)) = NaN; end;
			   try, radius     = cell2mat({chans.radius});     catch, radius(1:length(chans)) = NaN; end;
			   try, sph_theta  = cell2mat({chans.sph_theta});  catch, sph_theta(1:length(chans)) = NaN; end;
			   try, sph_phi    = cell2mat({chans.sph_phi});    catch, sph_phi(1:length(chans)) = NaN; end;
			   try, sph_radius = cell2mat({chans.sph_radius}); catch, sph_radius(1:length(chans)) = NaN; end;
			   eval(tmpoper)
			   chans = struct('labels', { chans.labels }, 'X', mat2cell(X), 'Y', mat2cell(Y), 'Z', mat2cell(Z), ...
							  'theta', mat2cell(theta), 'radius', mat2cell(radius), 'sph_theta', mat2cell(sph_theta), ...
							  'sph_phi', mat2cell(sph_phi), 'sph_radius', mat2cell(sph_radius));
               if     ~isempty(findstr(tmpoper, 'X')),          chans = convertlocs(chans, 'cart2all'); end;
               if     ~isempty(findstr(tmpoper, 'Y')),          chans = convertlocs(chans, 'cart2all'); end;
               if     ~isempty(findstr(tmpoper, 'Z')),          chans = convertlocs(chans, 'cart2all'); end;
               if     ~isempty(findstr(tmpoper, 'sph_theta')),  chans = convertlocs(chans, 'sph2all');
               elseif ~isempty(findstr(tmpoper, 'theta')),      chans = convertlocs(chans, 'topo2all'); end;
               if     ~isempty(findstr(tmpoper, 'sph_phi')),    chans = convertlocs(chans, 'sph2all');  end;
               if     ~isempty(findstr(tmpoper, 'sph_radius')), chans = convertlocs(chans, 'sph2all');
               elseif ~isempty(findstr(tmpoper, 'radius')),     chans = convertlocs(chans, 'topo2all'); end;
		   else 
			   eval(tmpoper);
		   end;
           
		  case 'shrink'
		   chans(1).shrink = args{ curfield+1 };		   
           
		  case 'plotrad'
		   chans(1).plotrad = args{ curfield+1 };		   
           
		  case 'delete'
		   chans(args{ curfield+1 })=[];
           
		  case 'changefield'
		   tmpargs = args{ curfield+1 };
		   if length( tmpargs ) < 3
			   error('pop_chanedit: not enough arguments to change field value');
		   end;
		   eval([ 'chans(' int2str(tmpargs{1}) ').'  tmpargs{2} '=' reformat(tmpargs{3} ) ';' ]);
           
		  case { 'insert' 'add' }
		   tmpargs = args{ curfield+1 };
		   allfields = fieldnames(chans);
		   if length( tmpargs ) < length(allfields)+1
			   error('pop_chanedit: not enough arguments to change all field values');
		   end;
		   num = tmpargs{1};
		   chans(end+1) = chans(end);
		   chans(num+1:end) = chans(num:end-1);
		   for index = 1:length( allfields )
               eval([ 'chans(' int2str(num) ').' allfields{index} '=' reformat(tmpargs{index+1}) ';' ]);
		   end;
           
		  case 'append'
		   tmpargs = args{ curfield+1 };
		   allfields = fieldnames(chans);
		   if length( tmpargs ) < length(allfields)+1
			   error('pop_chanedit: not enough arguments to change all field values');
		   end;
		   num = tmpargs{1};
		   chans(end+1) = chans(end);
		   chans(num+2:end) = chans(num+1:end-1);
		   for index = 1:length( allfields )
               eval([ 'chans(' int2str(num+1) ').' allfields{index} '=' reformat(tmpargs{index+1}) ';' ]);
		   end;
           
		  case 'changechan'
		   tmpargs = args{ curfield+1 };
		   num = tmpargs{1};
		   allfields = fieldnames(chans);
		   if length( tmpargs ) < length(allfields)+1
			   error('pop_chanedit: not enough arguments to change all field values');
		   end;
		   for index = 1:length( allfields )
			   eval([ 'chans(' int2str(num) ').' allfields{index} '=' reformat(tmpargs{index+1}) ';' ]);
		   end;
           
		  case 'load'
		   tmpargs = args{ curfield+1 };
		   if ~isempty(tmpargs), 
			   if isstr(tmpargs)
				   chans = readlocs(tmpargs);
               else 
                   chans = readlocs(tmpargs{:});
			   end;
		   end;
           
		  case 'lookup'
           tmplocs = readlocs('Standard-10-5-Cap385.sfp', 'filetype', 'sfp');
           [tmp ind1 ind2] = intersect(lower({ tmplocs.labels }), lower({ chans.labels }));
           if ~isempty(tmp)
               [ind2 ind3] = sort(ind2);
               ind1 = ind1(ind3);
               for index = 1:length(ind2)
                   chans(ind2(index)).theta      = tmplocs(ind1(index)).theta;
                   chans(ind2(index)).radius     = tmplocs(ind1(index)).radius;
                   chans(ind2(index)).X          = tmplocs(ind1(index)).X;
                   chans(ind2(index)).Y          = tmplocs(ind1(index)).Y;
                   chans(ind2(index)).Z          = tmplocs(ind1(index)).Z;
                   chans(ind2(index)).sph_theta  = tmplocs(ind1(index)).sph_theta;
                   chans(ind2(index)).sph_phi    = tmplocs(ind1(index)).sph_phi;
                   chans(ind2(index)).sph_radius = tmplocs(ind1(index)).sph_radius;
               end;
               tmpdiff = setdiff([1:length(chans)], ind2);
               if ~isempty(tmpdiff)
                   fprintf('Channel lookup: no location for ');
                   for index = 1:(length(tmpdiff)-1)
                       fprintf('%s,', chans(tmpdiff(index)).labels);
                   end;
                   fprintf('%s\nSend us standard location for your channels at eeglab@sccn.ucsd.edu\n', ...
                           chans(tmpdiff(end)).labels);
               end;
           end;
          case 'eval'
		   tmpargs = args{ curfield+1 };
           eval(tmpargs);
		  
          case 'save'
		   tmpargs = args{ curfield+1 };
		   if isempty(tmpargs), return; end;
		   fid = fopen(tmpargs, 'w');
		   if fid ==-1, error('Cannot open file'); end;
		   allfields = fieldnames(chans);
		   fprintf(fid, 'Number\t');
		   for field = 1:length(allfields)
			   fprintf(fid, '%s\t', allfields{field});
		   end;
		   fprintf(fid, '\n');
		   for index=1:length(chans)
			   fprintf(fid, '%d\t',  index);
			   for field = 1:length(allfields)
				   tmpval = getfield(chans, {index}, allfields{field});
				   if isstr(tmpval)
					   fprintf(fid, '%s\t',  tmpval);
				   else
					   fprintf(fid, '%3.3g\t',  tmpval);
				   end;
			   end;
			   fprintf(fid, '\n');
		   end;
		   if isempty(tmpargs), chantmp = readlocs(tmpargs); end;
		 end;
	 end;
end;

if isfield(chans, 'sph_phi_besa'  ), chans = rmfield(chans, 'sph_phi_besa'); end;
if isfield(chans, 'sph_theta_besa'), chans = rmfield(chans, 'sph_theta_besa'); end;
return;

function str = commandwarning(str);

% format the output field
% -----------------------
function strval = reformat( val )
    if isnumeric(val) & isempty(val), val = '[]'; end;
	if isstr(val), strval = [ '''' val '''' ];
	else           strval = num2str(val);
	end;

function txt = inserttxt( txt, tokins, tokfind);
	locfind = findstr(txt, tokfind);
	for index = length(locfind):-1:1
		txt = [txt(1:locfind(index)-1) tokins txt(locfind(index):end)];
	end;
function num = popask( text )
	 ButtonName=questdlg2( text, ...
	        'Confirmation', 'Cancel', 'Yes','Yes');
	 switch lower(ButtonName),
	      case 'cancel', num = 0;
	      case 'yes',    num = 1;
	 end;
function [chans, shrinkorskirt, plotrad]= checkchans(chans, fields);	

    % shrink and skirt factors
    % ------------------------
    shrinkorskirt = 0;
	plotrad  = [];
	if isfield(chans, 'plotrad'), 
		plotrad = chans(1).plotrad; 
		chans = rmfield(chans, 'plotrad');
        if isstr(plotrad) & ~isempty(str2num(plotrad)), plotrad = str2num(plotrad); end;
	end;
	if isfield(chans, 'shrink') 
		shrinkorskirt = 1;
        if ~isstr(chans(1).shrink)
            plotrad = 0.5/(1-chans(1).shrink); % convert old values
        end;
		chans = rmfield(chans, 'shrink');
	end;
    
    for index = 1:length(fields)
        if ~isfield(chans, fields{index})
            chans = setfield(chans, {1}, fields{index}, []);
        end;
    end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -