function [Value,RespObj] = calcopt(Property,RespObj,Ind)
%CALCOPT calculates Plot Options for Singular Value 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.
%
%   [Value,RespObj] = CALCOPT(Property,RespObj) also returns the 
%   Response Object with the Property value set to the calculated
%   value.

%   $Revision: 1.6 $  $Date: 2000/06/14 20:04:08 $
%   Copyright 1986-2000 The MathWorks, Inc. 
%   Karen Gondoly 1-28-97.

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

if nargin<3,
   Ind = 1:length(ResponseHandles);   
end

% Peak response is the only supported Characteristic
Value = struct('System',cell(length(Ind),1),'Frequency ',[],'Peak',[]);

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

RespObj.PeakResponseValue = Value;
