force a scientific notation format

61 Ansichten (letzte 30 Tage)
Sam Alex
Sam Alex am 25 Dez. 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);
  4 Kommentare
José-Luis
José-Luis am 25 Dez. 2012
Bearbeitet: José-Luis am 25 Dez. 2012
Please have a look here. And your question did not annoy me, I was just posting a suggestion.
As a general rule, people will be more inclined to answer a well posed, clear question. When I see unedited text posed as a question I often tend to ignore it, unless it is very short (like yours was). Other contributors might be more dedicated, but I think a nice presentation increases your chances for an answer.
neo
neo am 25 Dez. 2012
Bearbeitet: neo am 25 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

José-Luis
José-Luis am 25 Dez. 2012
Bearbeitet: José-Luis am 25 Dez. 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')
  4 Kommentare
Sam Alex
Sam Alex am 25 Dez. 2012
Thank you José-Luis
José-Luis
José-Luis am 25 Dez. 2012
My pleasure

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jonathan Epperl
Jonathan Epperl am 25 Dez. 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
  1 Kommentar
José-Luis
José-Luis am 25 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by