How do I specify number format in a plot axis in Matlab R2015a?

85 views (last 30 days)
I have a plot in Matlab R2015a which defaults to having y-axis labels formatted in exponential notation.
As standard practice I like to go in and reformat the axis labels to have consistent precision (which is an unfortunate shortcoming of pre-2016 plots). I do this as follows:
function fixaxislabels( xfrmt,yfrmt )
%fixaxislabels redefines the x and y axis labels to have consistent
% precision IAW xfrmt and yfrmt
xlabel=get(gca,'XTickLabel')
for i=1:numel(xlabel)
xval=str2num(xlabel{i});
xlabel{i}=num2str(xval,xfrmt);
end
ylabel=get(gca,'YTickLabel')
for i=1:numel(ylabel)
yval=str2num(ylabel{i});
ylabel{i}=num2str(yval,yfrmt);
end
xlabel
ylabel
set(gca,'XTickLabel',xlabel);
set(gca,'YTickLabel',ylabel);
However, this only recognizes the actual labels and returns labels between 1.000 and 10.000 rather than 0.001 and 0.010.
How do I force my axis labels to be fixed point notation before I normalize the label precision?

Answers (1)

Rik
Rik on 19 Oct 2017
Edited: Rik on 19 Oct 2017
Maybe you should get the actual values, instead of the labels. That would save you a conversion from string to number and solve this problem.
xlabel=get(gca,'XTick');%returns a vector
Edit: you can do this directly by setting the format for your ticklabels. But alas, this function was introduced in R2016b, so your release doesn't have it.
xtickformat(xfrmt)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!