function delselection(h)

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


%% Create transaction and get the handle to the event recorder
recorder = tsguis.recorder;
T = tsguis.transaction;

%% Delete selected data
for k=1:length(h.Waves)
    data = h.Waves(k).DataSrc.Timeseries.Data;
    time = h.Waves(k).DataSrc.Timeseries.Time;
    I = [];
    if strcmp(h.State,'DataSelect') && ...
            all(size(h.Waves(k).View.SelectedPoints)==size(data))
         I = h.Waves(k).View.SelectedPoints;
         % Clear selected for deleted points
%          h.Waves(k).View.SelectedPoints = ...
%              logical(false*ones(size(h.Waves(k).View.SelectedPoints)));
         h.Waves(k).View.SelectedPoints = [];
    elseif strcmp(h.State,'TimeSelect')
        I = false(size(data,1),1);
        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 | (time>=min(h.Waves(k).View.SelectedTimes(j,:)) & ...
               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
        I = logical(double(I)*ones([1 size(data,2)]));
        if strcmp(recorder.Recording,'on')
            h.SelectionStruct.History = [h.SelectionStruct.History; ...
                      {['I' h.Waves(k).Name ' = logical(double(I' ...
                      h.Waves(k).Name ')*ones([1,' ...
                      sprintf('%d',size(data,2)) ']));']}];
        end
        % Clear selection
        h.Waves(k).View.selectedtimes = [];
    end    
    
    if ~isempty(I) && any(any(I))
        data(I) = NaN;
        h.Waves(k).DataSrc.Timeseries.Data = data;
        drawnow % Flush the event queue before activating

        % If the recorder is on, cache the M code in the transaction buffer
        if strcmp(recorder.Recording,'on')
            T.addbuffer('%% Replacing data with NaNs');
            if isempty(h.SelectionStruct.History)            
                varname = addbuffervar(recorder,I);             
                T.addbuffer(sprintf('%s.Data(%s) = NaN;', ...
                    h.Waves(k).DataSrc.Timeseries.Name,varname),h.Waves(k).DataSrc.Timeseries);
            else
                for row=1:length(h.SelectionStruct.History)
                    T.addbuffer(h.SelectionStruct.History{row});
                end 
                T.addbuffer(sprintf('%s.Data(I%s) = NaN;',h.Waves(k).Name,h.Waves(k).Name),h.Waves(k).DataSrc.Timeseries);
                h.SelectionStruct.History = {};
            end
        end
    end
end

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


                  