function varargout = subsref(h, S)
%SUBSREF  Overload

%   Author(s): Rong Chen
%   Copyright 1986-2003 The MathWorks, Inc. 
%   $Revision: 1.1.6.1 $  $Date: 2004/12/26 21:35:21 $

if strcmp(S(1).type,'()')       
    % TS(IND) 
    % for ts object, only one dimensional subreferencing is allowed
    if length(S(1).subs)==1 
        if isempty(S(1).subs{1})
            varargout{1}=[];
            return
        elseif islogical(S(1).subs{1})
            I = find(S(1).subs{1});
        elseif ischar(S(1).subs{1})  
            % ':'
            if S(1).subs{1}==':'
                I = 1:getGridSize(h,1);
            else
                varargout{1}=[];
                return
            end
        elseif ~isempty(S(1).subs{1}) && isreal(S(1).subs{1})
            % any index
            I = unique(S(1).subs{:});
            if any(I<1) || any(I>getGridSize(h,1)) || ~isequal(round(I),I)
                error('Invalid indicies')
            end
        else
            varargout{1}=[];
            return
        end       

        % @BasicArray getslice without 'cell' returns array size [prod(SizeA)/prod(SampleSize) 1
        % Samplesize] (line 20 @ValueArray/getSlice)
        data = getSlice(h.Data_(1),{I});

        % Create output @timeseries
        tsout = tsdata.timeseries;
        time = getSlice(h.Data_(2),{I});
        if h.datainfo.gridfirst==1
            init(tsout,hdsReshapeArray(data,[length(I) h.Data_(1).SampleSize]), time);
        else
            init(tsout,hdsReshapeArray(data,[h.Data_(1).SampleSize length(I)]), time);
        end
        % Copy metadata
        set(get(tsout,'timeInfo'),'Units', h.timeInfo.Units, 'Format', h.timeInfo.Format, ...
            'Startdate', h.timeInfo.Startdate);
        tsout.dataInfo = h.dataInfo.copy;
        tsout.qualityInfo = h.qualityInfo.copy;

        % Copy quality
        tsout.Quality = getSlice(h.Data_(3),{I});

        % Slice events
        etimes = cell2mat(get(h.Events,{'Time'}));
        I = (etimes>=time(1) & etimes<=time(end));
        if any(I)
            addevent(tsout,h.Events(I));
        end

        % If there are more subref arguments call the subsref
        % method on the time series with the remaining arguments
        if length(S)>1
            varargout{1} = subsref(tsout,S(2:end));
        else
            varargout{1} = tsout;
        end
    else
        error('timeseries:subsref:scalarind', ...
            'Time series can only be indexed with a single dimension')
    end
else   
    % TS.Fieldname
    if nargout>0
        varargout = cell(1,nargout);
        varargout{:} = builtin('subsref',h,S);
    else
       builtin('subsref',h,S)
    end 
end




        