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

조회 수: 41 (최근 30일)
Matt Brown
Matt Brown 2017년 10월 18일
편집: Rik 2017년 10월 19일
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?

답변 (1개)

Rik
Rik 2017년 10월 19일
편집: Rik 2017년 10월 19일
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)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by