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

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

if length(S)==1 && strcmp(S.type,'()') 
    % TS(IND)
    % Determine which members have been selected

    % first dimension: sample index -- I
    % second dimension: time series object index/names -- J
    % get J
    if length(S.subs)==2
        if islogical(S.subs{2})
            J = find(S.subs{2});
        elseif isnumeric(S.subs{2})        
            if (any(S.subs{2}<1) || any(S.subs{2}>length(h.getMembers)) || ...
                      ~isequal(round(S.subs{2}),S.subs{2}))
                 error('Invalid member indicies')
            end
             J = S.subs{2};
        elseif iscell(S.subs{2})
            membernames = S.subs{2};
            ind = strcmp(membernames,h.getMembers);
            if ~any(ind)
                error('One or more member time series not found')
            end
            J = find(ind);
        elseif ischar(S.subs{2})
            if S.subs{2}~=':'
                ind = strcmp(S.subs{2},h.getMembers);
                if ~any(ind)
                    error('One or more member time series not found')
                end
                J = find(ind);
            else
                J = 1:length(h.getMembers);
            end
        end
    elseif length(S.subs)==1
        J = 1:length(h.getMembers);
    else
        error('1 or 2 dimensional sub referencing only')
    end
    % get I
    if ischar(S.subs{1}) 
        % : case
        if S.subs{1}==':'
            I = 1:length(h.Time);
        else
            error('Unrecognizable first dimention. It should be a vector containing the indices of the samples.')
        end
    elseif ~isempty(S.subs{1}) && isreal(S.subs{1})
        I = unique(S.subs{1});
        if isnumeric(I) && (any(I<1) || any(I>h.timeinfo.Length) || ~isequal(round(I),I))
            error('Invalid indicies')
        elseif islogical(I)
            I = find(I);
        end
    else
        return
    end
    
    % Intitilialize new @tscollection
    tsout = tsdata.tscollection;
    initialize(tsout,getSlice(h.Time_,{I}));

    % Copy metadata
	set(tsout.timeInfo,'Units', h.timeInfo.Units, 'Format', h.timeInfo.Format, ...
        'Startdate', h.timeInfo.Startdate); 
    
    % Add subreferenced @timeseries one at a time
    memberVars=h.getMembers;
    for k=1:length(J)
        thists = get(h,memberVars{J(k)});
        subsasgn(tsout,struct('type','.','subs',memberVars{J(k)}),...
            subsref(thists,struct('type','()','subs',{{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
    % TS.Fieldname
    if nargout>0
        varargout = cell(1,nargout);
        varargout{:} = builtin('subsref',h,S);
    else
        builtin('subsref',h,S);
    end 
end



        