function select(h,arg1,arg2)

%% Select the points on the specifed time series or axes. In the case where
%% arg1 is a time series arg2 is a logical array of the same size as the 
%% ordinate data which defined which points are selected. If arg 1 is an 
%% axes then arg 2 is an ordered pair defining the extent of the selected
%% rectangle
recorder = tsguis.recorder;

if isa(arg1,'tsdata.timeseries')
    
    % Parse args
    I = arg2;
    ts = arg1;
    
    %% Find the Wave for the specified time series
    idx = [];
    tsList = h.getTimeSeries;
    for k=1:length(tsList)
        if tsList{k} == ts
            idx = k;
            break
        end
    end

    %% Set the selected points in each view to the specified logical array
    if ~isempty(idx)
        L = h.Waves(idx).View.Curves; 
        h.Waves(idx).View.selectedpoints = I;
    end
elseif isa(handle(arg1),'axes')
    % parse args
    selected_rect = arg2;
    ax = arg1;
    % Allow sequential selection
    set(h.axesgrid,'NextPlot','add');

    % Find the enclosed data
    for k=1:length(h.Waves)
        L = findobj(h.Waves(k).View.Curves,'Parent',ax);
        if isempty(h.Waves(k).View.selectedpoints)
           s1 =  [size(h.Waves(k).Data.Amplitude,1), ...
                      length(h.Waves(k).View.Curves)];
           h.Waves(k).View.selectedpoints = false(s1);   
           % If macro recording, create string to generate index matrix
           if strcmp(recorder.Recording,'on')
              h.SelectionStruct.History = [h.SelectionStruct.History;...
                 {['I' h.Waves(k).Name ' = false([ ' num2str(s1) ']);' ]}];
           end
        end
        for j=1:length(L)
            xdata = get(L(j),'xdata');
            ydata = get(L(j),'ydata');
            xdata = xdata(:);
            ydata = ydata(:);        
            I = (xdata>=min(selected_rect(:,1)) & xdata<=max(selected_rect(:,1)) & ydata>=min(selected_rect(:,2)) & ...
                ydata<=max(selected_rect(:,2)));
            % The jth value is not necessarily the jth curve in the view
            % since the curves may be spread over different axes
            idx = find(L(j)==h.Waves(k).View.Curves);
            % Create a string to select points for the kth time series and
            % idx position
            if strcmp(recorder.Recording,'on')                   
                newhistory = {['newind = (' h.Waves(k).Name '.Time >= ' sprintf('%f',min(selected_rect(:,1))) ');']; ...
                          ['newind = newind & (' h.Waves(k).Name '.Time <= ' sprintf('%f',max(selected_rect(:,1))) ');']; ...
                          ['newind = newind & (' h.Waves(k).Name '.Data(:,' sprintf('%d',idx) ') >= ' sprintf('%f',min(selected_rect(:,2))) ');']; ...
                          ['newind = newind & (' h.Waves(k).Name '.Data(:,' sprintf('%d',idx) ') <= ' sprintf('%f',max(selected_rect(:,2))) ');']};
            end
            % If this selection stacks previous selections 'or' it with
            % those
            if size(h.Waves(k).View.selectedpoints(:,idx)) == size(I)
                h.Waves(k).View.selectedpoints(:,idx) = ...
                    h.Waves(k).View.selectedpoints(:,idx) | I;
                % Record the string representing the change in selection
                % status of the idx position
                if strcmp(recorder.Recording,'on')
                    h.SelectionStruct.History = [h.SelectionStruct.History; ...
                        newhistory; {['I' h.Waves(k).Name '(:,' sprintf('%d',idx) ...
                        ') = newind | I' h.Waves(k).Name  '(:,' sprintf('%d',idx) ');']}];            
                end             
            else
                h.Waves(k).View.selectedpoints(:,idx) = I;
                % Record the string representing the change in selection
                % status of the idx position
                if strcmp(recorder.Recording,'on')
                    h.SelectionStruct.History = [h.SelectionStruct.History; newhistory; ...
                        {'I' h.Waves(k).Name '(:,' sprintf('%d',idx)  ') = newind'}];                        
                end
            end 

        end
    end
end

%% Refresh
h.draw