function tscollout = horzcat(ts1,varargin)
%HORZCAT Overload horisontal concatonation 

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

if nargin==1
    tscollout = ts1;
    return
else
    ts2=varargin{1};
end

% Merge time vectors onto a common basis
[ts1timevec, ts2timevec,outprops] = ...
    timemerge(ts1.timeInfo,ts2.timeInfo,ts1.time,ts2.time);
 
% Check that the time vectors match
intervalLen = ((ts1timevec(end)-ts1timevec(1))/length(ts1timevec));
if norm(ts1timevec-ts2timevec)/intervalLen>1e-6
    error('Time vectors do not match')
end

% Build output tscollection
tscollout = tsdata.tscollection;
initialize(tscollout,ts1timevec);
set(tscollout.timeInfo,'Startdate',outprops.ref,'Units',outprops.outunits,'Format',outprops.outformat);

% Check for collisions
commonts = intersect(ts1.getMembers,ts2.getMembers);
for k=1:length(commonts)
   if ~isequal(get(get(ts1,commonts),'Data'),get(get(ts2,commonts),'Data'))
      errstr = sprintf('%s%s%s','The common member time series ', commonts{k},...
                    ' does not have common ordinate data');
      error(errstr)
   end
end

% Create the following table
%    Name      index (ts1 or ts2)
%   
%    uniquets  J                  => timeseries  = intimeseries(J(k))

J = [ones(1,length(ts1.getMembers)) 2*ones(1,length(ts2.getMembers))];
[uniquets, I] = unique([ts1.getMembers,ts2.getMembers]);
intimeseries = {ts1, ts2};

% Add concatonated timeseries one at a time
for k=1:length(uniquets)
    thists = get(intimeseries{J(I(k))},uniquets{k});
    ts = tsdata.timeseries(thists.Data);
    
    % Set timeseries metadata and quality
    set(ts,'dataInfo',thists.DataInfo,'qualityInfo',thists.qualityInfo,'Quality',thists.Quality);
    % update this ts object
    % set(ts,'Time',tscollout.Time,'TimeInfo',tscollout.TimeInfo.copy,'name',thists.Name);
    tmp=ts.getContainer('Time');
    tmp=tscollout.getTimeContainer;
    ts.timeinfo=tscollout.timeinfo;
    ts.name=thists.Name;

    % Add events
    addevent(ts,thists.Events);
    
    % Add new property and assign it 
    p = schema.prop(tscollout,uniquets{k},'handle');
    set(tscollout,uniquets{k},ts);

end














