MATLAB: How to format the labels of the axes so that numbers always appear with three digits, including leading zeroes (navigation format) in MATLAB 7.10 (R2010a)

MATLAB

I need to format the axis of a plot so that the numbers always appear with three digits.
The number 7 should appear as 007, 26 as 026, 155 as 155.

Best Answer

To display the labels on the axes in the desired format, use the following code (for non floating point numbers)
function s = formatinput1(n)
num = n;
s = cell(1,numel(n));
for i = 1:numel(num)
str = strtrim(num2str(num(i)));
if length(str) < 3
if length(str) == 1
strnew = strtrim('00');
strreq = strcat(strnew,str);
s{i} = strreq;
end
if length(str) == 2
strnew = strtrim('0');
strreq = strcat(strnew,str);
s{i} = strreq;
end
else
strreq = str;
s{i} = strreq;
end
end
end
s is a cell array returned by the function that contains the labels in the desired format, which can be used in conjunction with the XTickLabel property of the set function.
For floating point numbers, call the following function for each floating point number that is to be converted to the desired format.
function newstr = formatinput2(n)
str = strtrim(num2str(n));
[num1,num2] = strtok(str,'.');
ns = formatinput1(str2num(num1));
newstr = strcat(strtrim(ns),strtrim(num2));
end