www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/mbcmakelimits.m

    function lims = mbcmakelimits(data, opt)
%MBCMAKELIMITS Create min/max limits pair for data
%
%  LIMS = MBCMAKELIMITS(DATA) returns a [MIN, MAX] limits pair for DATA.
%  DATA can be a vector or multi-dimensional matrix.  MIN and  MAX will be
%  suitable for use as axes limits: MIN < MAX and they are both finite.
%
%  LIMS = MBCMAKELIMITS(DATA, ALGOPTION) specifies how the limits should be
%  calculated.  The default is 'tight' which bounds the min and max to the
%  extent of the data.  'loose' will add a buffer to the top and bottom and
%  then round to an appropriate number giving a result that is similar to
%  the builti-in axes behaviour.  'round' will widen the limits until they
%  are rounded to 2 significant figures.

%  Copyright 2000-2005 The MathWorks, Inc. and Ford Global Technologies, Inc.


F = isfinite(data);
if ~any(F(:))
    lims = [0 1];
else
    lims = [0 0];
    lims(1) = min(data(F));
    lims(2) = max(data(F));
    
    if (lims(2)-lims(1))<100*eps
        if abs(lims(1))<(50*eps)
            lims = [-1 1];
        else
            mid = mean(lims);
            if mid<0
                lims(2) = 0.9*mid;
                lims(1) = 1.1*mid;
            else
                lims(1) = 0.9*mid;
                lims(2) = 1.1*mid;
            end
        end
    else
        if nargin>1
            switch opt
                case 'loose'
                    % Perform additional processing to make nice limits
                    startlims = lims;

                    range = lims(2) - lims(1);
                    mag = (10.^floor(log10(abs(range/2))));
                    lims(1) = floor(lims(1)./mag).*mag;
                    lims(2) = ceil(lims(2)./mag).*mag;

                    % If this process has not opened a big enough gap at the top
                    % and bottom, widen it slightly
                    if (startlims(1)-lims(1))<(mag*eps)
                        lims(1) = lims(1) - 0.5*mag;
                    end
                    if (lims(2)-startlims(2))<(mag*eps)
                        lims(2) = lims(2) + 0.5*mag;
                    end
                
                case 'round'
                    % Round the limits up/down to 2 significant figures
                    NotIsZero = (lims~=0);
                    mag = [1 1];
                    mag(NotIsZero)  = 10.^(floor(log10(abs(lims(NotIsZero))))-1);
                    lims(1) = mag(1).*floor(lims(1)./mag(1));
                    lims(2) = mag(2).*ceil(lims(2)./mag(2));
                     
            end
        end
    end
end