function createResponse(this,View,Systems,Styles,varargin)
%CREATERESPONSE  Creates one response per system for a given plot.

%   Authors: Kamesh Subbarao
%   Copyright 1986-2012 The MathWorks, Inc. 

PlotType = View.Tag;
for ct=1:length(Systems)
   src = Systems(ct);
   r = View.addresponse(src);

   switch PlotType
      case {'step','impulse'}
         DefinedCharacteristics = src.getCharacteristics(PlotType);
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {@LocalTimeDataFcn this src PlotType r};
         p = varargin{1};  Config = varargin{2};
         if isempty(Config)
            Config = RespConfig();
         end
         r.Context = struct('Type',PlotType,'Time',[],'Parameter',p,'Config',Config);
      case 'initial'
         DefinedCharacteristics = src.getCharacteristics(PlotType);
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {@LocalTimeDataFcn this src PlotType r};
         if isempty(varargin)
            p = [];
         else
            p = varargin{1};
         end
         % Note: IC resolution deferred to createView
         Config = struct('InitialState',[]);
         r.Context = struct('Type','initial','Time',[],'Parameter',p,'Config',Config);
      case 'lsim'
         % varargin = u,t,x0,p,InterpRule
         DefinedCharacteristics = src.getCharacteristics(PlotType);
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {'lsim' src r};
         % Note: Input data and IC resolution deferred to createView
         if numel(varargin)<4
            p = [];
         else
            p = varargin{4};
         end
         r.Context = struct('InputIndex',[],'IC',[],'Parameter',p);
      case {'bode','bodemag'}
         DefinedCharacteristics = src.getCharacteristics('bode');
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {@LocalMagPhaseDataFcn src r 'bode' this};
      case 'nichols'
         DefinedCharacteristics = src.getCharacteristics(PlotType);
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {@LocalMagPhaseDataFcn src r 'nichols' this};
      case {'nyquist','sigma'}
         DefinedCharacteristics = src.getCharacteristics(PlotType);
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {@LocalFreqDataFcn src r PlotType this};
      case {'sectorplot','dirindex'} % REVISIT
         DefinedCharacteristics = src.getCharacteristics(PlotType);
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {@LocalFreqDataFcn src r PlotType this};
      case 'pzmap'
         DefinedCharacteristics = src.getCharacteristics(PlotType);
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {'pzmap' src r};
      case 'iopzmap'
         DefinedCharacteristics = src.getCharacteristics(PlotType);
         r.setCharacteristics(DefinedCharacteristics);
         r.DataFcn = {'pzmap' src r 'io'};
   end
   % Styles and preferences
   initsysresp(r,PlotType,View.Options)
   r.Style = Styles(ct);
end

%%%%%%%%%%%%%%%%%%%%
% LocalTimeDataFcn %
%%%%%%%%%%%%%%%%%%%%
function LocalTimeDataFcn(this,src,PlotType,r)
% Note: This indirection is needed to react to change in time vector, in
% particular for sparse models.
r.Context.Time = this.Preferences.TimeVector*tunitconv(this.Preferences.TimeVectorUnits,src.getTimeUnits);
timeresp(src,PlotType,r)

%%%%%%%%%%%%%%%%%%%%%%%%
% LocalMagPhaseDataFcn %
%%%%%%%%%%%%%%%%%%%%%%%%
function LocalMagPhaseDataFcn(src,r,PlotType,this)
% Data function for Frequency Mag, Phase plots
f = this.Preferences.FrequencyVector;
if iscell(f)
   % Frequency range specified
    fc = [f{:}]*funitconv(this.Preferences.FrequencyVectorUnits,'rad/TimeUnit',src.getTimeUnits);
   magphaseresp(src,PlotType,r,{fc(1) fc(2)})
else
   % f is [] or a vector
   magphaseresp(src,PlotType,r,f*funitconv(this.Preferences.FrequencyVectorUnits,'rad/TimeUnit',src.getTimeUnits));
end

%%%%%%%%%%%%%%%%%%%%
% LocalFreqDataFcn %
%%%%%%%%%%%%%%%%%%%%
function LocalFreqDataFcn(src,r,PlotType,this)
% Data function for Frequency Response plots
f = this.Preferences.FrequencyVector;
if iscell(f)
   % Frequency range specified
   fc = [f{:}]*funitconv(this.Preferences.FrequencyVectorUnits,'rad/TimeUnit',src.getTimeUnits);
   feval(PlotType,src,r,{fc(1) fc(2)});
else
   % f is [] or a vector
   feval(PlotType,src,r,f*funitconv(this.Preferences.FrequencyVectorUnits,'rad/TimeUnit',src.getTimeUnits));
end