classdef (CaseInsensitiveProperties = true, TruncatedProperties = true) gram < ltioptions.Generic
    % Options set for the GRAM command.
    
    % Copyright 2015 The MathWorks, Inc.
         
    properties
        % Frequency intervals to compute the controllability and
        % observability gramians (default = []).
        %
        % Frequency intervals are defined by a matrix with two columns.
        % Each row specifies a frequency interval with nonnegative
        % frequencies.
        FreqIntervals = [];
        
        % Time intervals to compute the controllability and
        % observability gramians (default = []).
        %
        % Time intervals are defined by a matrix with two columns.
        % Each row specifies a time interval with nonnegative
        % time values.
        TimeIntervals = [];        
    end       
    
    methods        
        function this = set.FreqIntervals(this,value)
            % SET method for FrequencyIntervals option
            this.FreqIntervals = setInterval(value,'FreqIntervals');       
        end
        
        function this = set.TimeIntervals(this,value)
            % SET method for TimeIntervals option
            this.TimeIntervals = setInterval(value,'TimeIntervals');
        end        
    end
    
    methods (Access = protected)
       function cmd = getCommandName(~)
          cmd = 'gram';
       end       
    end    
end

function w = setInterval(w,OptionName)
if ~isempty(w)
    % number check
    if ~(isnumeric(w) && isreal(w) && (size(w,2)==2) && all(all(w>=0)) && ismatrix(w))
        error(message('Control:transformation:gramoptions1',OptionName))
    end
    % boundary check
    w1 = w(:,1);
    w2 = w(:,2);
    if any(w(:,1)>=w(:,2))
        error(message('Control:transformation:gramoptions2',OptionName))
    end
    % overlap check
    if length(w2)>1 % multiple intervals
        [w1,ix]=sort(w1);
        w2=w2(ix);
        wtmp = [w1 w2]';
        if any(diff(wtmp(:))<0)
            error(message('Control:transformation:gramoptions3',OptionName))
        end
    end
    w = double(w);
end
end