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

📄 pop_editeventfield.m

📁 含有多种ICA算法的eeglab工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
									  'Edit event field(s) -- pop_editeventfield()', { EEG.eventdescription{:} '' } );        if length(results) == 0, return; end;	    % decode top inputs	    % -----------------	    args = {};	    if ~isempty( results{1} ), args = { args{:}, 'indices', results{1} }; end;	    if results{2} == 1       , args = { args{:}, 'delold', 'yes' }; end;	    	    % dealing with existing fields	    %-----------------------------	    for index = 1:length(allfields) 	        if results{index*2+2} == 1, args = { args{:}, allfields{index}, [] };	        else 				if ~isempty( results{index*2+1} )                    if exist(results{index*2+1}) == 2,  args = { args{:}, allfields{index}, [ results{index*2+1} ] }; % file	                else                                args = { args{:}, allfields{index}, results{index*2+1} }; end;				end;				try, 					if ~strcmp( userdat{index}, EEG.eventdescription{index})						args = { args{:}, [ allfields{index} 'info' ], userdat{index} }; 					end;				catch, end;	        end;     	    end;	    	    % dealing with the new field	    %---------------------------	    sub = 3;	    if ~isempty( results{end-sub} )	        if ~isempty( results{end-sub+1} )	            if exist(results{end-sub+1}) == 2,  args = { args{:}, results{end-sub}, [ results{end-sub+1} ] }; % file	            else                                args = { args{:}, results{end-sub}, results{end-sub+1} }; end;	            if ~isempty( userdat{end} ),        args = { args{:}, [ results{end-sub} 'info' ], userdat{end} }; end;	        else	            disp(['The new field' results{end-sub} ' was ignored since no input data were given for it.' ]);	        end;	    end;          % handle rename         % -------------        if results{end-1} ~= 1, args = { args{:}, 'rename', [ allfields{results{end-1}-1} '->' results{end} ] }; end;          if length(args) < 3, return; end;else % no interactive inputs    args = varargin;    % scan args to modify array/file format    % array are transformed into string     % files are transformed into string of string    % (this is usefull to build the string command for the function)    % --------------------------------------------------------------    for index=1:2:length(args)        if iscell(args{index+1}), args{index+1} = { args{index+1} }; end; % double nested         if isstr(args{index+1})                 args{index+1} = args{index+1}; % string         else if ~isempty( inputname(index+2) ), args{index+1} = inputname(index+2); end;        end;    end;                end;% create structure% ----------------if ~isempty(args)   try, g = struct(args{:});   catch, disp('Setevent: wrong syntax in function arguments'); return; end;else    g = [];end;% test the presence of variables% ------------------------------try, g.skipline;      catch, g.skipline = 0; end;try, g.indices;       catch, g.indices = [1:length(EEG.event)]; end;try, g.delold; 	      catch, g.delold = 'no'; end;try, g.timeunit; 	  catch, g.timeunit = 1; end;try, g.align; 	      catch, g.align = NaN; end;try, g.delim; 	      catch, g.delim = char([9 32]); end;g.align.val = g.align;if isstr(g.indices), g.indices = eval([ '[' g.indices ']' ]); end;tmpfields = fieldnames(g);% scan all the fields of g% ------------------------for curfield = tmpfields'    if ~isempty(EEG.event), allfields = fieldnames(EEG.event);    else                    allfields = {}; end;    switch lower(curfield{1})       case { 'append' 'delold', 'fields', 'skipline', 'indices', 'timeunit', 'align', 'delim' }, ; % do nothing now       case 'rename',            if isempty( findstr('->',g.rename) ), disp('Set warning: bad syntax for rename'); end;            oldname = g.rename(1:findstr('->',g.rename)-1);            newname = g.rename(findstr('->',g.rename)+2:end);            indexmatch = strmatch(oldname, allfields);            if isempty(indexmatch), disp('Set warning: name not found for rename');             else                for index  = 1:length(EEG.event)                     eval([ 'EEG.event(index).' newname '=EEG.event(index).' oldname ';']);                  end;                    EEG.event = rmfield(EEG.event, oldname);            end;            if isfield(EEG, 'urevent')                disp('Warning: field name not renamed in urevent structure');            end;       otherwise, % user defined field command                  % --------------------------            infofield = findstr(curfield{1}, 'info');            if ~isempty(infofield) & infofield == length( curfield{1} )-3                % description of a field                % ----------------------                     fieldname = curfield{1}(1:infofield-1);                indexmatch = strmatch( fieldname, allfields);                if isempty( indexmatch )                    disp(['Setevent warning: Field ' fieldname ' not found to add description, ignoring']);                else                    EEG.eventdescription{indexmatch} = getfield(g, curfield{1});                end;            else                              % not an field for description                % ----------------------------      	            if isempty( getfield(g, curfield{1}) ) % delete	                 indexmatch = strmatch( curfield{1}, allfields);                     if isempty( indexmatch )                        disp(['Set warning: Field ''' curfield{1} ''' not found for deletion, ignoring']);                     else	                    EEG.event = rmfield(EEG.event, curfield{1}); 	                    allfields(indexmatch) = [];                        if isfield(EEG, 'urevent')                            fprintf('Warning: field ''%s'' not deleted from urevent structure\n', curfield{1}  );                        end;						try,							EEG.eventdescription(indexmatch) = [];						catch, end;	                 end;    	            else % interpret		            switch g.delold % delete old events		                case 'yes'		                      EEG.event = load_file_or_array( getfield(g, curfield{1}), g.skipline, g.delim );		                      allfields = { curfield{1} };                              EEG.event = eeg_eventformat(EEG.event, 'struct', allfields);                              EEG.event = recomputelatency( EEG.event, 1:length(EEG.event), EEG.srate, g.timeunit, g.align);                              EEG = eeg_checkset(EEG, 'makeur');		                 case 'no' % match existing fields		                           % ---------------------		                      tmparray = load_file_or_array( getfield(g, curfield{1}), g.skipline, g.delim );		                      if isempty(g.indices) g.indices = [1:size(tmparray(:),1)] + length(EEG.event); end;		                      		                      indexmatch = strmatch(curfield{1}, allfields);		                      if isempty(indexmatch) % no match		                          disp(['Set: creating new field ''' curfield{1} '''' ]);		                      end;                              try                                  EEG.event = setstruct(EEG.event, curfield{1}, g.indices, cell2mat(tmparray));                              catch,                                  error('Wrong size for input array');                              end;  							  if strcmp(curfield{1}, 'latency')								  EEG.event = recomputelatency( EEG.event, g.indices, EEG.srate, g.timeunit, g.align);							  end; 							  if strcmp(curfield{1}, 'duration')                                  for indtmp = 1:length(EEG.event)                                      EEG.event(indtmp).duration = EEG.event(indtmp).duration/EEG.srate;                                  end;							  end;                              if isfield(EEG, 'urevent')                                  disp('pop-editeventfield: updating urevent structure');                                  try                                      for indtmp = g.indices(:)'                                          tmpval      = getfield (EEG.event, {indtmp}, curfield{1});                                          EEG.urevent = setfield (EEG.urevent, {EEG.event(indtmp).urevent}, curfield{1}, tmpval);                                      end;                                  catch,                                      disp('Warning: problem while updating urevent structure');                                  end;                              end;		             end;	            end;	        end;          end;end;if isempty(EEG.event) % usefull 0xNB empty structure    EEG.event = [];end;EEG = eeg_checkset(EEG, 'eventconsistency');% generate the output command% ---------------------------com = sprintf('%s = pop_editeventfield( %s', inputname(1), inputname(1));for i=1:2:length(args)    if ~isempty( args{i+1} )        if isstr( args{i+1} ) com = sprintf('%s, ''%s'', %s', com, args{i}, str2str(args{i+1}) );        else    			if ~iscell( args{i+1} ) com = sprintf('%s, ''%s'', [%s]', com, args{i}, num2str(args{i+1}) );			else 				com = sprintf('%s, ''%s'', { ', com, args{i});				for index = 1:length( args{i+1}{1} ), com = sprintf('%s ''%s'' ', com, args{i+1}{1}{index}); end;				com = sprintf('%s }', com);			end;                            end;    else        com = sprintf('%s, ''%s'', []', com, args{i} );    end;end;com = [com ');'];% interpret the variable name% ---------------------------function str = str2str( array )	str = '';	for index = 1:size(array,1)		str = [ str ', ''' array(index,:) '''' ];	end;	if size(array,1) > 1		str = [ 'strvcat(' str(2:end) ')'];	else		str = str(2:end);	end;	return;% interpret the variable name% ---------------------------function array = load_file_or_array( varname, skipline, delim );	if exist(varname) == 2 % mean that it is a filename                           % --------------------------        array = loadtxt( varname );            else % variable in the global workspace         % --------------------------         array = evalin('base', varname);         if ~iscell(array)             array = mat2cell(array, ones(1, size(array,1)), ones(1, size(array,2)));         end;        end;     return;% update latency values% ---------------------function event = recomputelatency( event, indices, srate, timeunit, align);    if ~isfield(event, 'latency'), return; end;    for index = indices        event(index).latency = round(event(index).latency*srate*timeunit);    end;         % create new field names% ----------------------function epochfield = getnewfields( epochfield, nbfields )   count = 1;   while nbfields > 0       if isempty( strmatch([ 'var' int2str(count) ], epochfield ) )               epochfield =  { epochfield{:} [ 'var' int2str(count) ] };               nbfields = nbfields-1;       else    count = count+1;       end;                       end;     return;    function var = setstruct( var, fieldname, indices, values )    if exist('indices') ~= 1, indices = 1:length(var); end;    if length(values) > 1        for index = 1:length(indices)            var = setfield(var, {indices(index)}, fieldname, values(index));        end;    else        for index = 1:length(indices)            var = setfield(var, {indices(index)}, fieldname, values);        end;    end;return;

⌨️ 快捷键说明

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