function [Value,RespObj] = calcopt(Property,RespObj,varargin)
%CALCOPT calculates Plot Options for Initial Condition Response Plots
%   Value = CALCOPT(Property,RespObj) calculates the values of the
%   Plot Options specified by Property for all systems in the Response
%   Object, RespObj. The values are returned as the cell array, Value, 
%   where each cell represents the results for each system in RespObj.
%
%   Value = CALCOPT(Property,RespObj,Index) calculates the values for
%   only the systems in RespObj for the indices in Index.
% $Revision: 1.4 $

%   Copyright 1986-2000 The MathWorks, Inc. 
%   Karen Gondoly 1-28-97.

ResponseHandles = get(RespObj,'ResponseHandles');
SysNames = get(RespObj,'SystemNames');

if nargin>2,
   Ind = varargin{1};
else
   Ind = 1:length(ResponseHandles);   
end

% Only peak response is supported
Value = struct('System',cell(length(Ind),1),'Time',[],'Peak',[]);

for ctInd = 1:length(Ind);
   AllHandles = ResponseHandles{Ind(ctInd)}; % Handles for a particular system
   Value(ctInd).System = SysNames{Ind(ctInd)};
   Xvals = ones([size(AllHandles),size(AllHandles{1})]);
   Yvals = Xvals;
   for ctrow = 1:size(AllHandles,1),
      Handles = AllHandles{ctrow,1};
      %---Go through each model
      for ctModel = 1:prod(size(Handles)),
         ResponseLine = findobj(Handles{ctModel},'Tag','LTIresponseLines');
         X = get(ResponseLine,'Xdata');
         Y = get(ResponseLine,'Ydata');
         [garb,indMax]=max(abs(Y));
         Yvals(ctrow,1,ctModel) = Y(indMax);
         Xvals(ctrow,1,ctModel) = X(indMax);
      end % for ctModel
   end % for ctrow
   
   %---Store the calculated values into the structured array
   Value(ctInd).Time = Xvals;
   Value(ctInd).Peak = Yvals;
end % for ctInd

RespObj.PeakResponseValue = Value;

