function [nxInLimits,xMinInLimits,xMaxInLimits] = ...
    rangeTool2(x,y,xLowLimit,xHighLimit,yLowLimit,yHighLimit)
%MATLAB Code Generation Library Function

% Copyright 2019 The MathWorks, Inc.
%#codegen

% Returns the number, minimum, and maximum of elements of
% x(x>=xlo & x<=xhi) or x(x>=xlo & x<=xhi & y>=ylo & y<=yhi) when 6 inputs 
% are supplied. If n == 0, minx and maxx are arbitrarily set to
% zero. The inputs xlow and xhigh are optional as are ylow and yhigh. If 
% not supplied, xlow and xhigh effectively default to minimum and maximum 
% finite values in x while y is also finite, respectively.
% This function should be used with 2, 4, or 6 inputs. 2 inputs for no x
% bin limits supplied. 4 inputs for x bin limits supplied but no y bin 
% limits. 6 inputs for x and y bin limits supplied.

nx = coder.internal.indexInt(numel(x));
k = coder.internal.indexInt(1);
% Scan forward to the first finite value in range.
if nargin > 2
    while k <= nx && ~(xLowLimit <= x(k) && x(k) <= xHighLimit)
        k = k + 1;
    end
elseif isfloat(x)
    while k <= nx && (~isfinite(x(k)) || ~isfinite(y(k)))
        k = k + 1;
    end
end
if k > nx
    xMinInLimits = coder.internal.scalarEg(x);
    nxInLimits = coder.internal.indexInt(0);
else
    xMinInLimits = x(k);
    nxInLimits = coder.internal.indexInt(1);
end
xMaxInLimits = xMinInLimits;
for j = k+1:nx
    if (nargin == 2 && isfinite(x(j)) && isfinite(y(j))) || ...
            (nargin == 4 && xLowLimit <= x(j) && x(j) <= xHighLimit && isfinite(y(j))) || ...
            (nargin == 6 && xLowLimit <= x(j) && x(j) <= xHighLimit && ...
            yLowLimit <= y(j) && y(j) <= yHighLimit)
        if nargout > 1
            if x(j) < xMinInLimits
                xMinInLimits = x(j);
            elseif x(j) > xMaxInLimits
                xMaxInLimits = x(j);
            end
        end
        nxInLimits = nxInLimits + 1;
    end
end