function rmselection(h,varargin)

%% Deletes selected points and resets the selected status of deleted points

%% Get the data
xdata = h.Responses.DataSrc.Timeseries.Data;
ydata = h.Responses.DataSrc.Timeseries2.Data;

if strcmp(h.State,'DataSelect') && ...
    isequal(size(h.Responses.View.SelectedPoints),...
        [size(ydata,2) size(xdata,2) size(xdata,1)])
    
    % Create transaction and get the handle to the event recorder
    recorder = tsguis.recorder;
    T = tsguis.transaction;

    % Note that for >1x1 views the 
    % same observation may be referenced by multiple points: e.g.: 
    % points (1,1,10) and (2,1,10) both affect time series 2 at 
    % col 1 observation 10. If any of these points are marked for
    % removal then it occurs at all corresponding points
    I = h.Responses.View.SelectedPoints;
    % Clear selected for deleted points
    h.Responses.View.SelectedPoints = [];
    
     if ~isempty(I) && all(all(I))
        errordlg('Removing all points would create an empty time series',...
            'Time Series Tools','modal')
        return      
     elseif ~isempty(I) && any(any(any(I)))
       I1 = squeeze(any(I,1));
       % Remove the obs from any row where even one element has been
       % excluded
       if size(I1,1)>1 
           I1 = any(I1);
       end
       % Suspend warnings between updateing time series since they will
       % be temporarily out of sync between the two updates
       swarn = warning('off','all');
       % If this is a keep operation rather than a remove then take the
       % complement
       if nargin>=2 && strcmp(varargin{1},'complement')
           h.Responses.DataSrc.Timeseries.init(h.Responses.DataSrc.Timeseries.Data(I1',:),...
              h.Responses.DataSrc.Timeseries.Time(I1'));
       else
           h.Responses.DataSrc.Timeseries.init(h.Responses.DataSrc.Timeseries.Data(~I1',:),...
               h.Responses.DataSrc.Timeseries.Time(~I1'));
       end
       I2 = squeeze(any(I,2));
       % Remove the obs from any row where even one element has been
       % excluded
       if size(I2,1)>1
           I2 = any(I2);
       end
       % If this is a keep operation rather than a remove then take the
       % complement
       if nargin>=2 && strcmp(varargin{1},'complement')
           h.Responses.DataSrc.Timeseries2.init(h.Responses.DataSrc.Timeseries2.Data(I2',:),...
               h.Responses.DataSrc.Timeseries2.Time(I2'));
       else    
           h.Responses.DataSrc.Timeseries2.init(h.Responses.DataSrc.Timeseries2.Data(~I2',:),...
               h.Responses.DataSrc.Timeseries2.Time(~I2'));
       end
       if any(strcmp({swarn.('state')},'on'))
           warning on;
       end
     else
       return
     end
    % Store transaction
    T.commit;
    recorder.pushundo(T);

end
                  