www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/@xregmultigraph2d/private/pr_genaxisticklabels.m

    function axisLab = pr_genaxisticklabels(axisVal, nChar)
%PR_GENAXISTICKLABELS Generate axis tick labels
%
%   AXISLAB = PR_GENAXISTICKLABELS(AXISVAL, NCHAR) generates axis tick
%   labels for the supplied axis values. Each label should be a maximum of
%   NCHAR characters long, not counting the decimal point.

%   Copyright 2006 The MathWorks, Inc.

% Ensure tick labels are at least 4 chars long
nChar = ceil(nChar);
nChar(nChar < 4) = 4;

% Convert axisVal to a row vector
axisVal = axisVal(:)';

% Cache form strings
formCache = i_genFormStr(nChar);

% Do in a for loop as it is quicker than cellfun
szVal = size(axisVal);
axisLab = cell(szVal);
for i = 1:szVal(2)
    axisLab{i} = i_genString(axisVal(i), formCache);
end

% Remove any trailing zeros after the decimal point. 
% Explanation of the regular expression : (?<=\.\d*[1-9])0*$
% The expression in parentheses is a lookahead operator. The expression
% that will be matched must follow an expression of the form 
% <Decimal point><Any number of digits><Digit between 1-9>.
% The expression outside the parentheses is the one to be matched. This is
% any number of trailing zeros anchored at the end of the string.
axisLab = regexprep(axisLab, '(?<=\.\d*[1-9])0+$', '');

% Remove + and zeros from exponent, e.g. 1e+007 becomes 1e7
axisLab = regexprep(axisLab, '+(\w*)0', ''); 

function axisStr = i_genString(axisVal, formCache)

mag = floor(log10(abs(axisVal))+1);
if abs(mag)<=4
    axisStr = sprintf(formCache{mag+5}, axisVal); 
else
    axisStr = sprintf(formCache{10}, axisVal);
end

function formCache = i_genFormStr(nChar)

formCache = cell(1, 10);
% Cache of format strings for numbers with exponents in range -4:4
for mag = 1:9
    formCache{mag} = sprintf('%%%d.%df', mag-5, nChar-mag+5 );
end
% Format string for all other numbers
formCache{10} = sprintf('%%0.%dg', nChar-3);