force a scientific notation format

250 views (last 30 days)
Sam Alex on 25 Dec 2012
hi,
when i use this code, instead of forcing it to give me the x-axis as: 0,1,2,...,10 then x10^3 (3 is a superscript) it gives me the numbers as 1000,2000,...and so on...any idea?
x = 0:0.1e4:0.1e5;
y = randn(11,1);
plot(x,y,'b-o')
set(gca,'xtick',0:0.1e4:0.1e5);
neo on 25 Dec 2012
I think following link may help:
I also feel this is a problem. The obvious solution would have been to edit XTickLabel using latex/tex interpreter. But Matlab help clearly states that one can not use latex/tex for axis tick labels. So solutions like above replaces them by text objects and then use latex/tex interpreter.
I Wonder how matlab does it for log-log or semilog plots or when axis limit exceeds about 10^5 or similar cases.

José-Luis on 25 Dec 2012
Edited: José-Luis on 25 Dec 2012
x = 0:0.1e4:0.1e5;
y = randn(11,1);
plot(x,y,'b-o')
xVals = 0:0.1e4:0.1e5;
set(gca,'xtick',xVals);
set(gca,'XTickLabel',sprintf('%2.0e|',xVals));
The last line in the previous snippet sets the format for your XTickLabels. For more information on other formats:
doc sprintf
EDIT Exponent should appear at the right of the X-axis
x = 0:0.1e4:0.1e5;
y = randn(11,1);
plot(x,y,'b-o')
xVals = 0:0.1e4:0.1e5;
set(gca,'xtick',xVals);
expVal = 3; %exponent you want
set(gca,'XTickLabel',sprintf('%2.0f|',xVals./(10^expVal)));
pos = get(gca,'Position');
offset = 0.00; %how far to the right you want the exponent
annotation('textbox',[pos(1)+ pos(3)+offset, pos(2), 0.2, 0.2],...
'String',['$\times 10^' num2str(expVal) '$'],...
'Interpreter','latex',...
'VerticalAlignment','bottom',...
'EdgeColor','none')
José-Luis on 25 Dec 2012
My pleasure

Jonathan Epperl on 25 Dec 2012
Afaik that's not possible, since the tick labels aren't like normal text objects in that they support a subset of latex commands. I recommend tick2text ( http://www.mathworks.com/matlabcentral/fileexchange/16003-tick2text-create-easy-to-customize-tick-labels ) from the FEX. Once you have it on your Matlab path:
x = 10.^(1:10);
plot(x,x)
g = gca;
tick2text(g,'axis','x')
h = getappdata(g,'XTickText')
for i=1:numel(h), set(h(i),'String',num2str(i,'10^%d')); end
José-Luis on 25 Dec 2012
What do you mean it's not possible? You can set XTickLabel to whatever string you want, even if it has nothing to do with the numeric value. You can't use Latex as the interpreter though, I'll agree to that, but that's not relevant for what the op asked, if I understood correctly.