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

📄 pop_importevent.m

📁 含有多种ICA算法的eeglab工具箱
💻 M
📖 第 1 页 / 共 2 页
字号:
	    if ~isempty(eval(results{end-2})), if eval(results{end-2}) ~= 0,  args = { args{:}, 'timeunit', eval(results{end-2}) }; end; end;	    % handle alignment 	    % ----------------     	    if ~isempty(eval(results{end-1})), if ~isnan(eval(results{end-1})),  args = { args{:}, 'align', eval(results{end-1}) }; end; end;	    if ~results{end} ~= 0,  args = { args{:}, 'optimalign', 'off' }; 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}), if iscell(args{index+1}{1}) args{index+1} = args{index+1}{1}; end; end; % double nested         if isstr(args{index+1}) & length(args{index+1}) > 2 & args{index+1}(1) == '''' & args{index+1}(end) == ''''                         args{index+1} = args{index+1}(2:end-1); end;        %else if ~isempty( inputname(index+2) ), args{index+1} = inputname(index+2); end;        %end;    end;                end;g = finputcheck( args, { 'fields'    'cell'     []         {};                         'skipline'  'integer'  [0 Inf]    0;                         'indices'   'integer'  [1 Inf]    [];                         'append'    'string'   {'yes' 'no' '''yes''' '''no''' }         'yes';                         'timeunit'  'real'     [0 Inf]    1;                         'event'     { 'real' 'string' }     []    [];                         'align'     'integer'  []         NaN;                         'optimalign' 'string'  { 'on' 'off' }         'on';                         'delim'     {'integer' 'string'}   []         char([9 32])}, 'pop_importevent');if isstr(g), error(g); end;if ~isempty(g.indices), g.append = 'yes'; end;g.delim = char(g.delim);    % test the presence of variables% ------------------------------%try, g.fields; 	 	  catch, g.fields = {}; end;%try, g.skipline;      catch, g.skipline = 0; end;%try, g.indices;  g.append = '''yes'''; catch, g.indices = []; end;%try, g.append; 	      catch, g.append = '''yes'''; 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;% determine latency for old event alignment% -----------------------------------------g.align.val = g.align;if ~isnan(g.align.val)    if isempty(EEG.event)        error('Setevent: no pre-existing event, cannot perform alignment');    end;        if ~isfield(EEG.event, 'latency')        error('Setevent: pre-existing events do not have a latency field for re-alignment');    end;        switch g.append        case {'yes' '''yes'''}, disp('Setevent warning: using align, events should not be appended but erased');    end;    if g.align.val < 0        g.align.event = EEG.event(1).latency;    else        g.align.event = EEG.event(g.align.val+1).latency;    end    g.align.nbevent = length(EEG.event);    g.oldevents = EEG.event;    g.align.txt = sprintf([ 'Check alignment between pre-existing (old) and loaded event' ...                          ' latencies:\nOld event latencies (10 first): %s ...\n' ], ...                          int2str(cell2mat({ EEG.event(1:min(10, length(EEG.event))).latency })));else    g.oldevents = [];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', 'fields', 'skipline', 'indices', 'timeunit', 'align', 'delim' }, ; % do nothing now        case 'event', % load an ascii file            switch g.append                 case { '''no''' 'no' } % ''no'' for backward compatibility                      EEG.event = load_file_or_array( g.event, g.skipline, g.delim );                      allfields = g.fields(1:min(length(g.fields), size(EEG.event,2)));                      EEG.event = eeg_eventformat(EEG.event, 'struct', allfields);					  % generate ori fields					  % -------------------					  for index = 1:length(EEG.event)						  EEG.event(index).init_index = index;						  EEG.event(index).init_time  = EEG.event(index).latency*g.timeunit;					  end;					  EEG.event = recomputelatency( EEG.event, 1:length(EEG.event), EEG.srate, ...                                                    g.timeunit, g.align, g.oldevents, g.optimalign);                case { '''yes''' 'yes' }                      % match existing fields                      % ---------------------                      tmparray = load_file_or_array( g.event, g.skipline, g.delim );                      if isempty(g.indices) g.indices = [1:size(tmparray,1)] + length(EEG.event); end;                      if length(g.indices) ~= size(tmparray,1)                            error('Set error: number of row in file does not match the number of event given as input');                       end;                      % add field                      % ---------                      g.fields = getnewfields( g.fields, size(tmparray,2)-length(g.fields));                                            % add new values                      % ---------------------                      for eventfield = 1:size(tmparray,2)                          if isstr(tmparray{1,eventfield})                              for indtmp = 1:length(g.indices)                                  EEG.event = setstruct( EEG.event, g.fields{eventfield}, g.indices(indtmp), tmparray{indtmp,eventfield});                              end;                          else EEG.event = setstruct( EEG.event, g.fields{eventfield}, g.indices, cell2mat(tmparray(:,eventfield)));                          end;                      end;      					  % generate ori fields					  % -------------------					  offset = length(EEG.event)-size(tmparray,2);					  for index = 1:size(tmparray,2)						  EEG.event(index+offset).init_index = index;						  EEG.event(index+offset).init_time  = EEG.event(index+offset).latency*g.timeunit;					  end;                      EEG.event = recomputelatency( EEG.event, g.indices, EEG.srate, g.timeunit, ...                                                    g.align, g.oldevents, g.optimalign);            end;      end;end;if isempty(EEG.event) % usefull 0xNB empty structure    EEG.event = [];end;% remove the events wit out-of-bound latencies% --------------------------------------------if isfield(EEG.event, 'latency')    try         res = cellfun('isempty', { EEG.event.latency });        res = find(res);        if ~isempty(res)            fprintf( 'Pop_importevent warning: %d/%d have invalid latencies and were removed\n', ...                     length(res), length(EEG.event));            EEG.event( res ) = [];        end;    end;	alllatencies = cell2mat( { EEG.event.latency } );	I1 = find(alllatencies < 0);	I2 = find(alllatencies > EEG.pnts*EEG.trials);	if (length(I1) + length(I2)) > 0 	    fprintf('Setevent warning: %d/%d events had out-of-bounds latencies and were removed\n', length(I1) + length(I2), length(EEG.event));	    EEG.event(union(I1, I2)) = [];	end;end;% generate ur variables% ---------------------EEG = eeg_checkset(EEG, 'eventconsistency');EEG = eeg_checkset(EEG, 'makeur');% generate the output command% ---------------------------com = sprintf('%s = pop_importevent( %s, %s);', inputname(1), inputname(1), vararg2str(args));% interpret the variable name% ---------------------------function array = load_file_or_array( varname, skipline, delim );    if isstr(varname) & exist(varname) == 2  % mean that it is a filename                                             % --------------------------        array = loadtxt( varname, 'skipline', skipline, 'delim', delim );            else % variable in the global workspace         % --------------------------         if isstr(varname)             array = evalin('base', varname);             if ~iscell(array)                 array = mat2cell(array, ones(1, size(array,1)), ones(1, size(array,2)));             end;             else             array = varname;         end;    end;     return;% update latency values% ---------------------function event = recomputelatency( event, indices, srate, timeunit, align, oldevents, optimalign);    % update time unit     % ----------------    if ~isfield(event, 'latency'),         if isfield(event, 'duration')            error('A duration field cannot be defined if a latency field has not been defined');        end;        return;     end;    for index = indices        event(index).latency  = event(index).latency*srate*timeunit;        if isfield(event, 'duration')            event(index).duration = event(index).duration*srate*timeunit;        end;    end;    % alignment with old events    % -------------------------    if ~isnan( align.val )        if align.val >= 0, alignlatency = event(1).latency;        else               alignlatency = event(-align.val+1).latency;        end;        for index = indices             event(index).latency = event(index).latency-alignlatency+align.event;        end;        if length(event) ~= align.nbevent            disp([ 'Setevent warning: the number of pre-existing events do not correspond to the ' ...                   'number of event that were read, so their latencies may have been wrongly re-aligned' ]);        end;                   fprintf(align.txt);        fprintf('New event latencies (10 first): %s ...\n', int2str(round(cell2mat({ event(1:min(10, length(event))).latency }))));    end;    if strcmpi(optimalign, 'on') & ~isempty(oldevents)        newlat = cell2mat({ event.latency     });        oldlat = cell2mat({ oldevents.latency });               newlat = repmat(newlat, [length(oldlat) 1]);        oldlat = repmat(oldlat', [1 size(newlat,2)]);        if align.val >= 0            newlat = newlat-newlat(1);            oldlat = oldlat-oldlat(1+align.val);        else            newlat = newlat-newlat(1-align.val);            oldlat = oldlat-oldlat(1);        end;                newfactor = fminsearch('eventalign',1,[],newlat, oldlat);        fprintf('Best sampling rate ratio found is %1.7f. Below latencies after adjustment\n', newfactor);        if newfactor > 1.01 | newfactor < 0.99            disp('Difference is more than 1%, something is wrong; ignoring ratio');            newfactor = 1;        end;                %diffarray = abs(newfactor*newlat-oldlat)';        %[allmins poss] = min(diffarray);        %figure; hist(allmins);    else        newfactor = 1;    end;    if ~isnan( align.val ) & newfactor ~= 1         if align.val >= 0            latfirstevent = event(1).latency;        else            latfirstevent = event(-align.val+1).latency;        end;        for index = setdiff(indices, 1)            event(index).latency = round(event(index).latency-latfirstevent)*newfactor+latfirstevent;        end;        if ~isempty(oldevents)            fprintf('Old event latencies (10 first): %s ...\n', int2str(round(cell2mat({ event(1:min(10, length(event))).latency }))));            fprintf('New event latencies (10 first): %s ...\n', int2str(round(cell2mat({ oldevents(1:min(10, length(oldevents))).latency }))));        end;    else        % must add one (because first sample point has latency 0        % ------------------------------------------------------        for index = indices            event(index).latency = round((event(index).latency+1)*1000*newfactor)/1000;        end;    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;    for index = 1:length(indices)        var = setfield(var, {indices(index)}, fieldname, values(index));    end;return;

⌨️ 快捷键说明

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