pop_select.m

来自「含有多种ICA算法的eeglab工具箱」· M 代码 · 共 571 行 · 第 1/2 页

M
571
字号
try, g.point;    catch, g.point = []; end; % will be set up latertry, g.nopoint;  catch, g.nopoint = []; end;try, g.channel;  catch, g.channel = [1:EEG.nbchan]; end;try, g.nochannel;        catch, g.nochannel = []; end;try, g.trialcond;        catch, g.trialcond = []; end;try, g.notrialcond;      catch, g.notrialcond = []; end;allfields = fieldnames(g);for index = 1:length(allfields)        switch allfields{index}         case { 'time' 'notime' 'trial' 'notrial' 'point' 'nopoint' 'channel' 'nochannel' 'trialcond' 'notrialcond' };         otherwise disp('pop_select error: unrecognized option'); beep; return;        end;end;g.trial   = setdiff( g.trial, g.notrial );g.channel = setdiff( g.channel, g.nochannel );if ~isempty(g.time) & (g.time(1) < EEG.xmin*1000) & (g.time(2) > EEG.xmax*1000)   error('Wrong time range');end;if min(g.trial) < 1 | max( g.trial ) > EEG.trials     error('Wrong trial range');end;if min(g.channel) < 1 | max( g.channel ) > EEG.nbchan     error('Wrong channel range');end;if size(g.point,2) > 2,     g.point = [g.point(1) g.point(end)];    disp('Warning: vector format for point range is deprecated');end;if size(g.nopoint,2) > 2,     g.nopoint = [g.nopoint(1) g.nopoint(end)];    disp('Warning: vector format for point range is deprecated');end;if ~isempty( g.point )    g.time = zeros(size(g.point));    for index = 1:length(g.point(:))        g.time(index) = eeg_point2lat(g.point(index), 1, EEG.srate, [EEG.xmin EEG.xmax]);    end;    g.notime = [];end;if ~isempty( g.nopoint )    g.notime = zeros(size(g.nopoint));    for index = 1:length(g.nopoint(:))        g.notime(index) = eeg_point2lat(g.nopoint(index), 1, EEG.srate, [EEG.xmin EEG.xmax]);    end;    g.time = [];end;if ~isempty( g.notime )    if size(g.notime,2) ~= 2        error('Time/point range must contain 2 columns exactly');    end;    if g.notime(2) == EEG.xmax        g.time = [EEG.xmin g.notime(1)];    else        if g.notime(1) == EEG.xmin            g.time = [g.notime(2) EEG.xmax];        elseif EEG.trials > 1            error('Wrong notime range. Remember that it is not possible to remove a slice of time for data epochs.');        end;    end;    if max(g.notime(:)) > EEG.xmax | min(g.notime(:)) < EEG.xmin        error('Time/point range out of data limits');    end;end;if ~isempty(g.time)    if size(g.time,2) ~= 2        error('Time/point range must contain 2 columns exactly');    end;    if max(g.time(:)) > EEG.xmax | min(g.time(:)) < EEG.xmin        error('Time/point range out of data limits');    end;end;% select trial values%--------------------if ~isempty(g.trialcond)   try, tt = struct( g.trialcond{:} ); catch      error('Trial conditions format error');   end;   ttfields = fieldnames (tt);   for index = 1:length(ttfields)        if ~isfield( EEG.epoch, ttfields{index} )            error([ ttfields{index} 'is not a field of EEG.epoch' ]);        end;    	    eval( [ 'Itriallow  = find( cell2mat({ EEG.epoch(:).' ttfields{index} ' }) >= tt.' ttfields{index} '(1) );' ] );	    eval( [ 'Itrialhigh = find( cell2mat({ EEG.epoch(:).' ttfields{index} ' }) <= tt.' ttfields{index} '(end) );' ] );	    Itrialtmp = intersect(Itriallow, Itrialhigh);	    g.trial = intersect( g.trial(:)', Itrialtmp(:)');   end;	   end;if isempty(g.trial)   error('Empty dataset, no trial');end;if length(g.trial) ~= EEG.trials	fprintf('Removing %d trial(s)...\n', EEG.trials - length(g.trial));end;if length(g.channel) ~= EEG.nbchan	fprintf('Removing %d channel(s)...\n', EEG.nbchan - length(g.channel));end;% recompute latency and epoch number for events% ---------------------------------------------if length(g.trial) ~= EEG.trials & ~isempty(EEG.event)    if ~isfield(EEG.event, 'epoch')        disp('Pop_epoch warning: bad event format with epoch dataset, removing events');        EEG.event = [];    else		if isfield(EEG.event, 'epoch')			keepevent = [];			for indexevent = 1:length(EEG.event)				newindex = find( EEG.event(indexevent).epoch == g.trial );				if ~isempty(newindex)					keepevent = [keepevent indexevent];					if isfield(EEG.event, 'latency')						EEG.event(indexevent).latency = EEG.event(indexevent).latency - (EEG.event(indexevent).epoch-1)*EEG.pnts + (newindex-1)*EEG.pnts;					end;					EEG.event(indexevent).epoch = newindex;				end;                			end;			diffevent = setdiff([1:length(EEG.event)], keepevent);			if ~isempty(diffevent)				disp(['Pop_select: removing ' int2str(length(diffevent)) ' unreferenced events']);				EEG.event(diffevent) = [];			end;    		end;    end;        end;% performing removal% ------------------if ~isempty(g.time) | ~isempty(g.notime)    if EEG.trials > 1        % select new time window        % ----------------------            try,   tmpeventlatency = cell2mat({EEG.event.latency});        catch, tmpeventlatency = [];        end;        alllatencies = 1-(EEG.xmin*EEG.srate); % time 0 point        alllatencies = linspace( alllatencies, EEG.pnts*(EEG.trials-1)+alllatencies, EEG.trials);        [EEG.data tmptime indices epochevent]= epoch(EEG.data, alllatencies, ...                                                     [g.time(1) g.time(2)]*EEG.srate, 'allevents', tmpeventlatency);        tmptime = tmptime/EEG.srate;        if g.time(1) ~= tmptime(1) & g.time(2)-1/EEG.srate ~= tmptime(2)            fprintf('pop_select(): 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);        alllatencies = alllatencies(indices);                % modify the event structure accordingly (latencies and add epoch field)        % ----------------------------------------------------------------------        allevents = [];        newevent = [];        count = 1;        if ~isempty(epochevent)            newevent = EEG.event(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;        end;        EEG.event = newevent;        EEG.epoch = [];    else        if isempty(g.notime)            g.time = g.time';            if g.time(1) ~= 0, g.notime = [0 g.time(1:end)'];            else               g.notime = [g.time(2:end)'];            end;            if g.time(end) == EEG.xmax, g.notime(end) = [];            else                        g.notime(end+1) = EEG.xmax;            end;                        for index = 1:length(g.notime)                if g.notime(index) ~= 0  & g.notime(index) ~= EEG.xmax                    if mod(index,2), g.notime(index) = g.notime(index) + 1/EEG.srate;                    else             g.notime(index) = g.notime(index) - 1/EEG.srate;                    end;                end;            end;                    g.notime = reshape(g.notime, 2, length(g.notime)/2)';        end;                   nbtimes = length(g.notime(:));        points = eeg_lat2point(g.notime(:)', ones(1,nbtimes), EEG.srate, [EEG.xmin EEG.xmax]);        points = reshape(points, size(g.notime));        EEG = eeg_eegrej(EEG, points);    endend;% performing removal% ------------------EEG.data      = EEG.data(g.channel, :, g.trial);EEG.trials    = length(g.trial);EEG.pnts      = size(EEG.data,2);EEG.nbchan    = length(g.channel);if ~isempty(EEG.chanlocs)    EEG.chanlocs = EEG.chanlocs(g.channel);end;    if ~isempty(EEG.epoch)   EEG.epoch = EEG.epoch( g.trial );end;if ~isempty(EEG.specdata)	if length(g.point) == EEG.pnts   		EEG.specdata = EEG.specdata(g.channel, :, g.trial);   	else   		EEG.specdata = [];   		fprintf('Warning: spectral data were removed because of the change in the numner of points\n');   	end;		end;% ica specific% ------------if ~isempty(EEG.icasphere)   EEG.icasphere = EEG.icasphere(:,g.channel);end;if ~isempty(EEG.icawinv)   EEG.icawinv = EEG.icawinv(g.channel,:);end;if ~isempty(EEG.icaact)	if length(g.channel) == size( EEG.icaact,1)   		EEG.icaact = [];		if ~isempty(EEG.specicaact)			if length(g.point) == EEG.pnts   				EEG.specicaact = EEG.specicaact(:, :, g.trial);   			else   				EEG.specicaact = [];		   		fprintf('Warning: spectral ICA data were removed because of the change in the numner of points\n');   			end;		   	end;   	else   		EEG.icaact = [];   		EEG.specicaact = [];   	end;		end;EEG = rmfield( EEG, 'reject');EEG.reject.rejmanual = [];% for stats, can adapt remove the selected trials and electrodes% in the future to gain time -----------------------------------  EEG = rmfield( EEG, 'stats');EEG.stats.jp = [];EEG = eeg_checkset(EEG, 'eventconsistency');% generate command% ----------------com = sprintf('EEG = pop_select( %s,%s);', inputname(1), vararg2str(args));return;% ********* OLD, do not remove any event any more% ********* in the future maybe do a pack event to remove events not in the time range of any epochif ~isempty(EEG.event)    % go to array format if necessary    if isstruct(EEG.event), format = 'struct';    else                     format = 'array';    end;    switch format, case 'struct', EEG = eventsformat(EEG, 'array'); end;        % keep only events related to the selected trials    Indexes = [];    Ievent  = [];    for index = 1:length( g.trial )        currentevents = find( EEG.event(:,2) == g.trial(index));        Indexes = [ Indexes ones(1, length(currentevents))*index ];        Ievent  = union( Ievent, currentevents );    end;    EEG.event = EEG.event( Ievent,: );    EEG.event(:,2) = Indexes(:);        switch format, case 'struct', EEG = eventsformat(EEG, 'struct'); end;end;

⌨️ 快捷键说明

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