function rmselection(h,varargin)

%% Removes (keeps) observations where any data is selected 

if length(h.Waves)==0
    return
end

%% Create transaction and get the handle to the event recorder
T = tsguis.transaction;
recorder = tsguis.recorder;
  
%% Find selected data and call tsreset to remove it
for k=1:length(h.Waves)
    data = h.Waves(k).DataSrc.Timeseries.Data;
    t = h.Waves(k).DataSrc.Timeseries.Time;
    I = [];
    if strcmp(h.State,'DataSelect') &&  ...
            all(size(h.Waves(k).View.SelectedPoints)==size(data))
        if size(data,2)>1
            I = any(h.Waves(k).View.SelectedPoints')';
            if strcmp(recorder.Recording,'on')
                h.SelectionStruct.History = [h.SelectionStruct.History;...
                    {['I' h.Waves(k).Name '= any(I' h.Waves(k).Name ''')'';']}];
            end      
        else
            I = h.Waves(k).View.SelectedPoints;        
        end
                
        % Clear selected for deleted points
          h.Waves(k).View.SelectedPoints = [];
    elseif strcmp(h.State,'TimeSelect')
        I = false(size(h.Waves(k).Data.Time));
        if strcmp(recorder.Recording,'on')
            h.SelectionStruct.History = {['I' h.Waves(k).Name ' = false([' ...
                num2str(size(h.Waves(k).Data.Time)) ']);']};
        end
        for j=1:size(h.Waves(k).View.SelectedTimes,1)
            I = I | (h.Waves(k).Data.Time>=min(h.Waves(k).View.SelectedTimes(j,:)) & ...
               h.Waves(k).Data.Time<max(h.Waves(k).View.SelectedTimes(j,:)));
            if strcmp(recorder.Recording,'on')
                h.SelectionStruct.History = [h.SelectionStruct.History; ...
                    {['I' h.Waves(k).Name ' = I' h.Waves(k).Name '| (' ...
                    h.Waves(k).Name '.Time>= ' sprintf('%f',min(h.Waves(k).View.SelectedTimes(j,:))) ...
                    ' & ' h.Waves(k).Name '.Time<' sprintf('%f',max(h.Waves(k).View.SelectedTimes(j,:))) ');']}];
            end
        end
        % Clear selection
        h.Waves(k).View.selectedtimes = [];
    end 
    
    % If this is a keep operation rather than a remove then take the
    % complement
    if nargin>=2 && strcmp(varargin{1},'complement')
        I = ~I;
        if strcmp(recorder.Recording,'on')
            h.SelectionStruct.History = [h.SelectionStruct.History; ...
                {'%% Retaining data - complementing selected time indices'};...
                {['I' h.Waves(k).Name ' = ~I' h.Waves(k).Name ';']}];
        end
    end
        
    if ~isempty(I) && all(I)
        errordlg('Removing all points would create an empty time series',...
            'Time Series Tools','modal')
        return
    elseif ~isempty(I) && any(I)
        h.Waves(k).DataSrc.Timeseries.init(data(~I,:),t(~I));
        thists = h.Waves(k).DataSrc.Timeseries; 
        % If the recorder is on cache the M code in the transaction buffer
        if strcmp(recorder.Recording,'on')
            T.addbuffer('%% Selecting data for removal');
            if ~isempty(h.SelectionStruct.History) 
                 for row=1:length(h.SelectionStruct.History)
                    T.addbuffer(h.SelectionStruct.History{row});
                 end 
                 T.addbuffer('%% Removing data');
                 T.addbuffer([h.Waves(k).Name '.init(' h.Waves(k).Name ...
                     '.Data(~I' h.Waves(k).Name ',:),' h.Waves(k).Name ...
                     '.Time(~I' h.Waves(k).Name '));'],thists);    
            else    
                varname = addbuffervar(recorder,~I);                             
                T.addbuffer(sprintf('%s.init(%s.Data(%s,:),%s.Time(%s);)',...
                    thists.Name,thists.Name,varname,thists.Name,varname),thists);
            end
        end
    else
        return
    end
    drawnow % Flush the event queue before activating
end

%% Store transaction
T.commit;
recorder.pushundo(T);