📄 pop_epoch.m
字号:
EEG.event(trial).latency = -EEG.xmin*EEG.srate+1+(trial-1)*EEG.pnts; end; else disp('Cannot epoch data with no events'); beep; return; end;end;if ~isfield(EEG.event, 'latency'), disp( 'Absent latency field in event array/strcuture: first rename one of the field to ''latency'''); beep; return;end; OLDEEG = EEG;if nargin < 3 % popup window parameters % ----------------------- promptstr = { strvcat('Time-locking event type(s) ([]=all):', ... 'Select ''Edit > Event values'' to see type values.'), ... 'Epoch limits [start, end] in seconds:', ... 'Name for the new dataset:', ... 'Out-of-bounds EEG rejection limits ([min max], []=none):' }; cbevent = ['if ~isfield(EEG.event, ''type'')' ... ' errordlg2(''No type field'');' ... 'else' ... ' if isstr(EEG.event(1).type)' ... ' tmpfieldnames = unique({EEG.event.type});' ... ' else' ... ' tmpstr = unique(cell2mat({EEG.event.type}));' ... ' tmpfieldnames = cell(1, length(tmpstr));' ... ' for tmps=1:length(tmpstr), tmpfieldnames{tmps} = num2str(tmpstr(tmps)); end;' ... ' end;' ... ' [tmps,tmpv, tmpstr] = listdlg2(''PromptString'',''Select event types'', ' ... ' ''ListString'', tmpfieldnames);' ... ' if tmpv' ... ' set(findobj(''parent'', gcbf, ''tag'', ''events''), ''string'', tmpstr);' ... ' end;' ... 'end;' ... 'clear tmps tmpv tmpstr tmpfieldnames;' ]; geometry = { [2 1 0.5] [2 1 0.5] [1.93 1.5] [2 1 0.5] }; uilist = { { 'style' 'text' 'string' 'Time-locking event type(s) ([]=all)' } ... { 'style' 'edit' 'string' '' 'tag' 'events' } ... { 'style' 'pushbutton' 'string' '...' 'callback' cbevent } ... { 'style' 'text' 'string' 'Epoch limits [start, end] in seconds' } ... { 'style' 'edit' 'string' '[-1 2]' } ... { } ... { 'style' 'text' 'string' 'Name for the new dataset' } ... { 'style' 'edit' 'string' fastif(isempty(EEG.setname), '', [ EEG.setname ' epochs' ]) } ... { 'style' 'text' 'string' 'Out-of-bounds EEG limits if any [min max]' } ... { 'style' 'edit' 'string' '' } { } }; result = inputgui( geometry, uilist, 'pophelp(''pop_epoch'')', 'Extract data epochs - pop_epoch()'); if length(result) == 0 return; end; if strcmpi(result{1}, '[]'), result{1} = ''; end; events = parsetxt( result{1} ); lim = eval( [ '[' result{2} ']' ] ); args = {}; if ~isempty( result{3} ), args = { args{:}, 'newname', result{3} }; end; if ~isempty( result{4} ), args = { args{:}, 'valuelim', eval( [ '[' result{4} ']' ] ) }; end; args = { args{:}, 'epochinfo', 'yes' }; else % no interactive inputs args = varargin;end;% create structure% ----------------if ~isempty(args) try, g = struct(args{:}); catch, disp('pop_epoch(): wrong syntax in function arguments'); return; end;else g = [];end;% test the presence of variables% ------------------------------try, g.epochfield; catch, g.epochfield = 'type'; end; % obsoletetry, g.timeunit; catch, g.timeunit = 'points'; end;try, g.verbose; catch, g.verbose = 'on'; end;try, g.newname; catch, g.newname = fastif(isempty(EEG.setname), '', [EEG.setname ' epochs' ]); end;try, g.eventindices; catch, g.eventindices = 1:length(EEG.event); end;try, g.epochinfo; catch, g.epochinfo = 'yes'; end;try, if isempty(g.valuelim), g.valuelim = [-Inf Inf]; end; catch, g.valuelim = [-Inf Inf]; end;% transform string events into a int array of column indices% ----------------------------------------------------------tmpeventlatency = cell2mat( { EEG.event(:).latency } );[tmpeventlatency Itmp] = sort(tmpeventlatency);EEG.event = EEG.event(Itmp); % sort in ascending time Ievent = g.eventindices;if ~isempty( events ) % select the events for epoching % ------------------------------ Ieventtmp = []; tmpeventtype = { EEG.event.type }; if iscell(events) if isstr(EEG.event(1).type) for index2 = 1:length( events ) tmpevent = events{index2}; if ~isstr( tmpevent ), tmpevent = num2str( tmpevent ); end; Ieventtmp = [ Ieventtmp ; strmatch(tmpevent, tmpeventtype, 'exact') ]; end; else for index2 = 1:length( events ) tmpevent = events{index2}; if isstr( tmpevent ),tmpevent = str2num( tmpevent ); end; if isempty( tmpevent ), error('pop_epoch(): string entered in a numeric field'); end; Ieventtmp = [ Ieventtmp find(tmpevent == cell2mat(tmpeventtype)) ]; end; end; else error('pop_epoch(): multiple event types must be entered as {a cell array}'); return; end; Ievent = Ieventtmp;end;% select event latencies for epochingIevent = sort(Ievent);alllatencies = tmpeventlatency(Ievent);if isempty(alllatencies) error('pop_epoch(): empty event range'); return;end;fprintf('pop_epoch():%d epochs selected\n', length(alllatencies));% select event time format and epoch% ----------------------------------switch lower( g.timeunit ) case 'points', [EEG.data tmptime indices epochevent]= epoch(EEG.data, alllatencies, [lim(1) lim(2)]*EEG.srate, ... 'valuelim', g.valuelim, 'allevents', tmpeventlatency); tmptime = tmptime/EEG.srate; case 'seconds', [EEG.data tmptime indices epochevent]= epoch(EEG.data, alllatencies, lim, 'valuelim', g.valuelim, ... 'srate', EEG.srate, 'allevents', tmpeventlatency); otherwise, disp('pop_epoch(): invalid event time format'); beep; return;end;alllatencies = alllatencies(indices);fprintf('pop_epoch():%d epochs generated\n', length(indices));% update other fields% -------------------if lim(1) ~= tmptime(1) & lim(2)-1/EEG.srate ~= tmptime(2) fprintf('pop_epoch(): time limits have been adjusted to [%3.3f %3.3f] to fit data points limits\n', tmptime(1), tmptime(2)+1/EEG.srate);end;EEG.xmin = tmptime(1);EEG.xmax = tmptime(2);EEG.pnts = size(EEG.data,2);EEG.trials = size(EEG.data,3);EEG.icaact = [];if ~isempty(EEG.setname) if ~isempty(EEG.comments) EEG.comments = strvcat(['Parent dataset "' EEG.setname '": ----------'], EEG.comments); end; EEG.comments = strvcat(['Parent dataset: ' EEG.setname ], ' ', EEG.comments);end;EEG.setname = g.newname;% count the number of events to duplicate and duplicate them% ----------------------------------------------------------totlen = 0;for index=1:EEG.trials, totlen = totlen + length(epochevent{index}); end;EEG.event(1).epoch = 0; % create the epoch field (for assignment consistency afterwards)if totlen ~= 0 newevent(totlen) = EEG.event(1); % reserv arrayelse newevent = [];end;% modify the event structure accordingly (latencies and add epoch field)% ----------------------------------------------------------------------allevents = [];count = 1;for index=1:EEG.trials for indexevent = epochevent{index} newevent(count) = EEG.event(indexevent); newevent(count).epoch = index; newevent(count).latency = newevent(count).latency - alllatencies(index) - tmptime(1)*EEG.srate + 1 + EEG.pnts*(index-1); count = count + 1; end;end;EEG.event = newevent;EEG.epoch = [];EEG = eeg_checkset(EEG, 'eventconsistency');% check for boundary events% -------------------------disp('pop_epoch(): checking epochs for data discontinuity');if ~isempty(EEG.event) & isstr(EEG.event(1).type) boundaryindex = strmatch('boundary', { EEG.event.type }); if ~isempty(boundaryindex) indexepoch = []; for tmpindex = boundaryindex indexepoch = [indexepoch EEG.event(tmpindex).epoch ]; end; EEG = pop_select(EEG, 'notrial', indexepoch); end;end;% generate text command% ---------------------com = sprintf('%s = pop_epoch( %s, { ', inputname(1), inputname(1));for j=1:length(events); if isstr( events{j} ) com = sprintf('%s ''%s'' ', com, events{j} ); else com = sprintf('%s [%s] ', com, num2str(events{j}) ); end;end;com = sprintf('%s }, [%s]', com, num2str(lim)); for i=1:2:length(args) if ~isempty( args{i+1} ) if isstr( args{i+1} ) com = sprintf('%s, ''%s'', ''%s''', com, args{i}, args{i+1} ); else com = sprintf('%s, ''%s'', [%s]', com, args{i}, num2str(args{i+1}) ); end; end; end;com = [com ');'];return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -