Filter löschen
Filter löschen

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

27 Ansichten (letzte 30 Tage)
Matt Brown
Matt Brown am 18 Okt. 2017
Bearbeitet: Rik am 19 Okt. 2017
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?

Antworten (1)

Rik
Rik am 19 Okt. 2017
Bearbeitet: Rik am 19 Okt. 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)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by