How do I change the x-axis label on a SEMILOGX plot from exponential to normal format in MATLAB?

105 views (last 30 days)
I have a semilogx plot and I do not want my axis labels to be in exponential format.
Using the following code:
x= 0:.2:2;
semilogx(10.^x,x)
Gives my x-axis labels as:
[10^0 10^1 10^2]
and instead I want:
[1 10 100]

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Jul 2013
'XTickLabel' of an axes is of type 'char' and 'XTick' is of type double. Whenever, a semi-log plot is created using SEMILOG function, XTickLabels by default will be shown in exponential form. However, XTicks are stored as doubles and if these Xtick values are assigned back as XTickLabels, the new XTickLabels will not be in exponential format, because of double to char conversion.
The x-axis labels can be changed with the following code:
New_XTickLabel = get(gca,'xtick');
set(gca,'XTickLabel',New_XTickLabel);
These custom XTickLabels do not update dynamically when you zoom into the plot, though the XTicks themselves update. Therefore, the above code has to be executed on each zoom operation. Using the ACTIONPOSTCALLBACK property of the zoom handle, we can execute the above commands after every zoom operation is performed and update the TickLabels to reflect the new axes limits.
Therefore, the code is now :
x= 0:.2:2;
figure
semilogx(10.^x,x)
New_XTickLabel = get(gca,'xtick');
set(gca,'XTickLabel',New_XTickLabel);
zoomH = zoom(gcf);
set(zoomH,'ActionPostCallback',{@zoom_mypostcallback});
The ACTIONPOSTCALLBACK function for the zoom handle is defined as :
function zoom_mypostcallback( ~,~ )
% This function executes after every zoom operation
New_XTickLabel = get(gca,'xtick');
set(gca,'XTickLabel',New_XTickLabel);
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!