📄 pop_editeventvals.m
字号:
set(objevent, 'string', num2str(valnum)); % update commands % --------------- oldcom = { oldcom{:} 'delete', valnum }; end; case { 'assign' 'changefield' }, % ********************************************************** if gui, % GUI case field = tmparg; objfield = findobj('parent', gcf, 'tag', field); editval = get(objfield, 'string'); if ~isempty(editval) & ~isempty(str2num(editval)), editval = str2num(editval); end; % update history % -------------- oldcom = { oldcom{:} 'changefield' { valnum field editval }}; else % command line case valnum = tmparg{1}; field = tmparg{2}; editval = tmparg{3}; end; % latency and duration case % ------------------------- if strcmp( field, 'latency') & ~isempty(editval) if isfield(EEG.event, 'epoch') editval = eeg_lat2point( editval, EEG.event(valnum).epoch, ... EEG.srate, [EEG.xmin EEG.xmax]*1000, 1E-3); else editval = (editval- EEG.xmin)*EEG.srate+1; end; end; if strcmp( field, 'duration') & ~isempty(editval) if isfield(EEG.event, 'epoch') editval = editval/1000*EEG.srate; % milliseconds else editval = editval*EEG.srate; % seconds end; end; % adapt to other formats % ---------------------- EEG.event(valnum) = setfield(EEG.event(valnum), field, editval); EEG.event = checkconsistency(EEG.event, valnum, field); % update urevents % --------------- if isfield(EEG, 'urevent') & isfield(EEG.event, 'urevent') urvalnum = EEG.event(valnum).urevent; % latency case % ------------ if strcmp( field, 'latency') & ~isempty(editval) if isfield(EEG.urevent, 'epoch') urepoch = EEG.urevent(urvalnum).epoch; % find closest event latency % -------------------------- if valnum<length(EEG.event) if EEG.event(valnum+1).epoch == urepoch urlatency = EEG.urevent(EEG.event(valnum+1).urevent).latency; latency = EEG.event(valnum+1).latency; end; end; if valnum>1 if EEG.event(valnum-1).epoch == urepoch urlatency = EEG.urevent(EEG.event(valnum-1).urevent).latency; latency = EEG.event(valnum-1).latency; end; end; % update event % ------------ if exist('urlatency') ~=1 disp('Urevent not updated: could not find other event in the epoch'); else editval = urlatency - ( latency - editval ); % new latency value end; else editval = eeg_urlatency(EEG.event, EEG.event(valnum).latency); end; elseif strcmp( field, 'latency') % empty editval EEG.event(valnum).latency = NaN; end; % duration case % ------------ if strcmp( field, 'duration') & ~isempty(editval) if isfield(EEG.event, 'epoch') editval = editval/1000*EEG.srate; % milliseconds -> point else editval = editval*EEG.srate; % seconds -> point end; end; EEG.urevent = setfield(EEG.urevent, {urvalnum}, field, editval); end; case 'sort', % ********************************************************** if gui % retrieve data field1 = get(findobj('parent', gcf, 'tag', 'listbox1'), 'value'); field2 = get(findobj('parent', gcf, 'tag', 'listbox2'), 'value'); dir1 = get(findobj('parent', gcf, 'tag', 'order1'), 'value'); dir2 = get(findobj('parent', gcf, 'tag', 'order2'), 'value'); if field1 > 1, field1 = allfields{field1-1}; else return; end; if field2 > 1, field1 = allfields{field2-1}; else field2 = []; end; % update history % -------------- oldcom = { oldcom{:} 'sort' { field1 dir1 field2 dir2 } }; else % command line field1 = tmparg{1}; if length(tmparg) < 2, dir1 = 0; else dir1 = tmparg{2}; end; if length(tmparg) < 3, field2 = []; else field2 = tmparg{3}; end; if length(tmparg) < 4, dir2 = 0; else dir2 = tmparg{4}; end; end; if ~isempty(field2) try, eval(['tmparray = cell2mat( { EEG.event.' field2 ' } );']); catch, eval(['tmparray = { EEG.event.' field2 ' };']); end; if strcmp(field2, 'latency') & EEG.trials > 1 tmparray = eeg_point2lat(tmparray, {EEG.event.epoch}, EEG.srate, [EEG.xmin EEG.xmax], 1); end; [X I] = mysort( tmparray ); if dir2 == 1, I = I(end:-1:1); end; events = EEG.event(I); else events = EEG.event; end; try, eval(['tmparray = cell2mat( { events.' field1 ' } );']); catch, eval(['tmparray = { events.' field1 ' };']); end; if strcmp( field1, 'latency') & EEG.trials > 1 tmparray = eeg_point2lat(tmparray, {events.epoch}, EEG.srate, [EEG.xmin EEG.xmax], 1); end; [X I] = mysort( tmparray ); if dir1 == 1, I = I(end:-1:1); end; EEG.event = events(I); if gui % warn user % --------- warndlg2('Sorting done'); else noeventcheck = 1; % otherwise infinite recursion with eeg_checkset end; end; % end switch end; % end loop % save userdata % ------------- if gui userdata{1} = EEG; userdata{2} = oldcom; set(gcf, 'userdata', userdata); pop_editeventvals('goto', shift); else if ~exist('noeventcheck') EEG = eeg_checkset(EEG, 'eventconsistency'); EEG = eeg_checkset(EEG, 'checkur'); end; end; return;end;% ----------------------% graphic interface part% ----------------------if isempty(EEG.event) disp('Getevent: cannot deal with empty event structure'); return;end; allfields = fieldnames(EEG.event);tmpind = strmatch('urevent', allfields);allfields(tmpind) = [];if nargin<2 % add field values % ---------------- geometry = { [2 0.5] }; tmpstr = sprintf('Edit event field values (currently %d events)',length(EEG.event)); uilist = { { 'Style', 'text', 'string', tmpstr, 'fontweight', 'bold' } ... { 'Style', 'pushbutton', 'string', 'Delete event', 'callback', 'pop_editeventvals(''delete'');' }}; for index = 1:length(allfields) geometry = { geometry{:} [1 1 1 1] }; % input string % ------------ if strcmp( allfields{index}, 'latency') | strcmp( allfields{index}, 'duration') if EEG.trials > 1 inputstr = [ allfields{index} ' (ms)']; else inputstr = [ allfields{index} ' (sec)']; end; else inputstr = allfields{index}; end; % callback for displaying help % ---------------------------- if index <= length( EEG.eventdescription ) tmptext = EEG.eventdescription{ index }; if ~isempty(tmptext) if size(tmptext,1) > 15, stringtext = [ tmptext(1,1:15) '...' ]; else stringtext = tmptext(1,:); end; else stringtext = 'no-description'; tmptext = 'no-description'; end; else stringtext = 'no-description'; tmptext = 'no-description'; end; cbbutton = ['questdlg2(' vararg2str(tmptext) ... ',''Description of field ''''' allfields{index} ''''''', ''OK'', ''OK'');' ]; % create control % -------------- cbedit = [ 'pop_editeventvals(''assign'', ''' allfields{index} ''');' ]; uilist = { uilist{:}, { }, ... { 'Style', 'pushbutton', 'string', inputstr, 'callback',cbbutton }, ... { 'Style', 'edit', 'tag', allfields{index}, 'string', '', 'callback', cbedit } ... { } }; end; % add buttons % ----------- geometry = { geometry{:} [1] [1.2 0.6 0.6 1 0.6 0.6 1.2] [1.2 0.6 0.6 1 0.6 0.6 1.2] [2 1 2] }; tpappend = 'Append event after the current event'; tpinsert = 'Insert event before the current event'; tporigin = 'Original index of the event (in EEG.urevent table)'; uilist = { uilist{:}, ... { }, ... { },{ },{ }, {'Style', 'text', 'string', 'Event Num', 'fontweight', 'bold' }, { },{ },{ }, ... { 'Style', 'pushbutton', 'string', 'Insert event', 'callback', 'pop_editeventvals(''append'', 0);', 'tooltipstring', tpinsert }, ... { 'Style', 'pushbutton', 'string', '<<', 'callback', 'pop_editeventvals(''goto'', -10);' }, ... { 'Style', 'pushbutton', 'string', '<', 'callback', 'pop_editeventvals(''goto'', -1);' }, ... { 'Style', 'edit', 'string', '1', 'callback', 'pop_editeventvals(''goto'', 0);', 'tag', 'numval' }, ... { 'Style', 'pushbutton', 'string', '>', 'callback', 'pop_editeventvals(''goto'', 1);' }, ... { 'Style', 'pushbutton', 'string', '>>', 'callback', 'pop_editeventvals(''goto'', 10);' }, ... { 'Style', 'pushbutton', 'string', 'Append event', 'callback', 'pop_editeventvals(''append'', 1);', 'tooltipstring', tpappend }, ... { }, { 'Style', 'text', 'string', ' ', 'tag', 'original' 'horizontalalignment' 'center' 'tooltipstring' tporigin } { } }; % add sorting options % ------------------- listboxtext = 'No field selected'; for index = 1:length(allfields) listboxtext = [ listboxtext '|' allfields{index} ]; end; geometry = { geometry{:} [1] [1 1 1] [1 1 1] [1 1.5 0.5] }; uilist = { uilist{:}, ... { 'Style', 'text', 'string', 'Re-order events (for review only)', 'fontweight', 'bold' }, ... { 'Style', 'text', 'string', 'Main sorting field:' }, ... { 'Style', 'listbox', 'string', listboxtext, 'tag', 'listbox1' }, ... { 'Style', 'checkbox', 'string', 'Click for decreasing order', 'tag', 'order1' } ... { 'Style', 'text', 'string', 'Secondary sorting field:' }, ... { 'Style', 'listbox', 'string', listboxtext, 'tag', 'listbox2' }, ... { 'Style', 'checkbox', 'string', 'Click for decreasing order', 'tag', 'order2' }, ... { 'Style', 'pushbutton', 'string', 'Re-sort', 'callback', 'pop_editeventvals(''sort'');' }, ... { }, { }}; userdata = { EEG {} }; inputgui( geometry, uilist, 'pophelp(''pop_editeventvals'');', ... 'Edit event values -- pop_editeventvals()', userdata, 'plot'); pop_editeventvals('goto', 0); % wait for figure % --------------- fig = gcf; waitfor( findobj('parent', fig, 'tag', 'ok'), 'userdata'); try, userdata = get(fig, 'userdata'); close(fig); % figure still exist ? catch, return; end; % transfer events % --------------- if ~isempty(userdata{2}) com = sprintf('%s = pop_editeventvals(%s,%s);', inputname(1), inputname(1), vararg2str(userdata{2})); end; if isempty(findstr('''sort''', com)) if ~isempty(userdata{2}) % some modification have been done EEG = userdata{1}; disp('Checking event consistency...'); EEG = eeg_checkset(EEG, 'eventconsistency'); EEG = eeg_checkset(EEG, 'checkur'); end; else com = ''; disp('WARNING: all edits discarded because of event resorting. The EEGLAB event structure'); disp(' must contain events sorted by latency (you may obtain an event structure'); disp(' with resorted event by calling this function from the command line).'); end; return; end;return;% format the output field% -----------------------function strval = reformat( val, latencycondition, trialcondition, eventnum) if latencycondition if trialcondition strval = ['eeg_lat2point(' num2str(val) ', EEG.event(' int2str(eventnum) ').epoch, EEG.srate,[EEG.xmin EEG.xmax]*1000, 1E-3);' ]; else strval = [ '(' num2str(val) '-EEG.xmin)*EEG.srate+1;' ]; end; else if isstr(val), strval = [ '''' val '''' ]; else strval = num2str(val); end; end;% sort also empty values% ----------------------function [X, I] = mysort(tmparray); if iscell(tmparray) if all(cellfun('isreal', tmparray)) tmpempty = cellfun('isempty', tmparray); tmparray(tmpempty) = { 0 }; tmparray = cell2mat(tmparray); end; end; try, [X I] = sort(tmparray); catch, sadf disp('Sorting failed. Check that selected fields contain uniform value format.'); X = tmparray; I = 1:length(X); end; % checkconsistency of new event% -----------------------------function eventtmp = checkconsistency(eventtmp, valnum, field) otherval = mod(valnum+1, length(eventtmp)+1)+1; if isstr(getfield(eventtmp(valnum), field)) & ~isstr(getfield(eventtmp(otherval), field)) eventtmp(valnum) = setfield(eventtmp(valnum), field, str2num(getfield(eventtmp(valnum), field))); end; if ~isstr(getfield(eventtmp(valnum), field)) & isstr(getfield(eventtmp(otherval), field)) eventtmp(valnum) = setfield(eventtmp(valnum), field, num2str(getfield(eventtmp(valnum), field))); end; if strcmpi(field, 'latency') & isempty(getfield(eventtmp(valnum), field)) eventtmp(valnum).latency = NaN; end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -