function addTs(h,ts1,ts2)

%% Check that the time series are compatable
if size(ts1.Data,1) ~= size(ts2.Data)
    msg = sprintf('Time series with differing numbers of samples cannot display on an xy view. Length of %s is %d. length of %s is %d.',...
        ts1.Name,ts1.timeInfo.Length,ts2.Name,ts2.timeInfo.Length);
    msg = [msg, 'Do you wish to merge the two time vectors in order to see the xy view.'];
    ButtonName = questdlg(msg, 'Time Series Tools', ...
                       'OK','Cancel','Cancel');
    if strcmp(ButtonName,'OK')               
        h.openmergedlg('modal')
    end
end

%% If there is an existing response remove it
rs = h.Responses;
for k=1:length(rs)
    h.rmresponse(rs(k));
end


%% Create new tssource
src = tsguis.tssource;
set(src,'Timeseries',ts1,'Timeseries2',ts2);

%% Size the xyplot to match the size of the time series data vectors
[NewOutputNames,NewInputNames] = src.getrcname;
resize(h,NewOutputNames,NewInputNames)

%% Make sure all new axes have the right behavior
h.setbehavior

%% Overwrite the axesgrid labels to remove the prefixes 'To:' and
%% 'From:'
set(h.axesgrid,'RowLabel',NewOutputNames,'ColumnLabel',NewInputNames)

%% Add the new response
r = h.addresponse(src);
r.datafcn = {@xyresp src r};

%% Add a listener to each @timeseries datachange event which fires the 
%% waveform datachanged event
r.addlisteners(handle.listener(ts1,'datachange', ...
    {@localDataChange r}));
r.addlisteners(handle.listener(ts2,'datachange', ...
    {@localDataChange r}));

%% Add a listener to each timeseries name property of the Plot Data
%% to update the axes labeling
r.addlisteners(handle.listener(ts1,ts1.findprop('Name'),'PropertyPostSet',...
    {@localDataChange r}));
r.addlisteners(handle.listener(ts2,ts2.findprop('Name'),'PropertyPostSet',...
    {@localDataChange r}));

%% Intialize plot editor
if isempty(h.PropEditor)
    h.PropEditor = tsguis.propeditor;
    h.edit(h.PropEditor);
end 

%% Update title
h.axesgrid.Title = sprintf('XY Plot of %s vs. %s',ts1.Name,ts2.Name);
h.axesgrid.Xlabel = ts1.Name;
S = warning('off','all'); % Disable "Some data is missing @resppack warn..."
h.draw
warning(S);

function localDataChange(eventSrc, eventData,r)

%% @timeseries datachange listener callback
S = warning('off','all'); % Disable "Some data is missing @resppack warn..."
r.DataSrc.send('SourceChanged');
warning(S);
