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

42 ビュー (過去 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 Center および File ExchangeAxis Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by