Change multiple exponent on x-axis.

I have a plot with x-values between 0.85*10^5 and 0.115*10^5. I want to display the x-values as multiples of 10^3. How can I do that?
I looked around on forums and found the following method:
xtick = get(gca, 'xtick') xticklabel = strread(sprintf('%.0f;', xtick), '%s', 'delimiter', ';'); set(gca,'xticklabel', xtick)
But this method just converts the values into integers.
Any ideas?

 Accepted Answer

Adam
Adam on 23 Mar 2016
Edited: Adam on 23 Mar 2016
If you are using R2015b or later take a look at:
hAxes.XAxis;
where hAxes would be your axes handle (e.g. hAxes = gca), which gives you a NumericRuler object, one of whose properties is Exponent.
hAxes.XAxis.Exponent = 3;
should, I think, achieve what you want. If you are working pre R2015b then I'm not sure how you would achieve this as I can't remember what axes properties were available/accessible then in place of this recent addition.

4 Comments

Thanks a lot for your answer. I am using R2015b, and at the moment I have:
plot(x_Q,y_Q)
title('Probability density function of the Load')
xlabel('Load [kN]')
xlim([85*10^3 115*10^3])
hAxex.XAxis.Exponent = 3;
This doesn't seem to work. Am I missing something?
Adam
Adam on 23 Mar 2016
Edited: Adam on 23 Mar 2016
Ah, sorry, I forgot to mention that in my answer, hAxes is your axes handle. So you need something like
hAxes = gca;
after you do your plot. (I also fixed the typo in my spelling of hAxex to hAxes).
I would generally advise that you create this axes handle prior to plotting and then plot explicitly onto that axes e.g.
figure; hAxes = gca;
plot( hAxes, x_Q,y_Q);
...
hAxes.XAxis.Exponent = 3;
In simple cases it doesn't matter, I just find it good practice. I also do the same for titles, xlabels and anything that is applied to the axes - most of these functions have a syntax that allows you to pass the axes handle as the first argument). If you end up working with multiple figures or more complex GUIs with axes it is much better to be explicit which axes to plot on rather than just rely on the axes you want being the current axes.
If you have the handle to the object created by the PLOT call, but not the handle to the axes in which that object was created, you can ensure you're modifying the correct axes using ANCESTOR.
% Plot into an axes
h = plot(1:10, 1:10);
% Pause so you can see the axes original color
pause
% Change the color of the axes to yellow
ax = ancestor(h, 'axes');
ax.Color = 'y';
This may not seem that useful with just one axes, but it becomes more useful with multiple axes because it avoids the possibility that which axes is the current axes may change. With this code, I could click on any other axes in any other figure while MATLAB is paused, and this will only change the color of the axes containing the line.
IT WORKS! Thanks a lot! :)

Sign in to comment.

More Answers (0)

Asked:

on 23 Mar 2016

Commented:

on 23 Mar 2016

Community Treasure Hunt

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

Start Hunting!