function rmselection(h)

%% Deletes selected points and resets the selected status of deleted points
if length(h.Waves)==0 || ~strcmp(h.State,'IntervalSelect')
    return;
end

%% Create transaction
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;

    % Collect indicies in any selected Y interval
    I = false(size(data,1),1);
    for j=1:size(h.Waves(k).View.SelectedInterval,1)
        J = data>=min(h.Waves(k).View.SelectedInterval(j,:)) & ...
           data<=max(h.Waves(k).View.SelectedInterval(j,:));
        if size(J,2)>1
            J = any(J')';
        end
        I = I | J;
    end
    % Update data
    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));
    else
        return
    end
    
    %% If macro recorder is on write the code
    if strcmp(recorder.Recording,'on')
        T.addbuffer(['I = false([size(' h.Waves(k).Name '.Data,1),1]);']);
        for j=1:size(h.Waves(k).View.SelectedInterval,1)
           T.addbuffer(['J =  ' h.Waves(k).Name '.Data>=' ...
               sprintf('%f',min(h.Waves(k).View.SelectedInterval(j,:))) ' & ' ...
               h.Waves(k).Name '.Data<=' ...
               sprintf('%f',max(h.Waves(k).View.SelectedInterval(j,:))) ';']);
           T.addbuffer('if size(J,2)>=2');
           T.addbuffer('   I = (I | any(J'')'');');
           T.addbuffer('else');
           T.addbuffer('   I = (I | J);');
           T.addbuffer('end');
        end
        T.addbuffer([h.Waves(k).Name '.init(' h.Waves(k).Name '.Data(~I,:),' ...
               h.Waves(k).Name '.Time(~I));'],h.Waves(k).DataSrc.Timeseries);
    end
    
    % Clear current selection
    h.Waves(k).View.SelectedInterval = [];  
end

%% Clear existing selections and reset limits
drawnow % Flush the event queue before activating
h.draw

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