Axis in scientific form - have tried to stop it....

axis in scientific form - have tried to stop it….
I have a plot with a logarithmic scale on the x-axis and I want it to say the numbers like 1,10,100,1000 etc. and not 10^0, 10^1, 10^2 etc. It'd be great if someone could help me do that.
I've given it about 20 minutes just now and I gave it 20 minutes yesterday too and I still can't get it to work. I've left my attempt on the bottom anyway.
close all
clear all
m = 0.05;
k = 1000;
R = 1;
f = linspace(0,19999,20000);
Xm = m.*(2*pi.*f) - k./(2*pi.*f);
createfigurelog(f,Xm);
%set(gca,'XTick', [0,1,10,100,1000,10000]); %set(gca,'XTickLabel',sprintf('%3.4f|',f))

 채택된 답변

Walter Roberson
Walter Roberson 2012년 1월 28일

5 개 추천

curtick = get(gca, 'XTick');
set(gca, 'XTickLabel', cellstr(num2str(curtick(:))));
The scientific notation is not used if the tick labels are set.
Also note the "trick" of passing a column vector to num2str.

댓글 수: 6

Tom
Tom 2012년 2월 6일
Cheers Walter.
can you explain in more detail what ur trick is? "Also note the "trick" of passing a column vector to num2str."
When you get() the current tick values you get out a row vector of numbers . You want to convert each of the numeric values into a member of a cell array of character vectors.
If you were to try use num2str on the numeric row vector then what you would get back would be a single character vector that had all of the values in it, separated by spaces, such as '1.5 3.0 4.5'. If you were to try to set the labels to that, MATLAB would copy the entire character vector for every label. MATLAB will not split the character vector at the spaces to get individual labels.
So to get out a cell array of character vectors, you have some choices:
  1. loop formatting one at a time and assigning into the cell array
  2. use arrayfun with 'uniform', 0
  3. use the undocumented but useful sprintfc
Or, convert the row vector into a column vector that you pass to num2str, getting out a character array with one row per entry, and then use cellstr to convert the character array to a cell array of character vectors.
Since R2016b there has also been options involving string objects.
Since R2015b the way to do this is recommended as
ax = gca;
ax.XRuler.Exponent = 0;
Setting the Exponent property to 0 turns off scientific notation.
I would have to research the corresponding methods for R2014b and R2015a (the graphics system changed in R2014b but not all the changes were ready in that release.)
Eric
Eric 2021년 2월 22일
편집: Eric 2021년 2월 22일
I may be making a rookie mistake, but this approach doesn't seem to work for loglog( ) in R2018b.
The command (ax.XRuler.Exponent = 0) executes without an error, with ax set to gca, but the axis properties don't change.
The other method with
get (gca, 'XTick')
etc. does still work with loglog( ) in R2018b.
The Exponent is intended to deal with the situation in which the end of the ruler has an exponent written in, such as
x = linspace(0,5,1000);
y = 100*exp(x).*sin(20*x);
ax = gca;
plot(ax, x, y)
ax.XRuler.TickLabel
ans = 6x1 cell array
{'0'} {'1'} {'2'} {'3'} {'4'} {'5'}
ax.XRuler.TickLabelFormat
ans = '%g'
However, when you use loglog() then those end-of-ruler exponents are not written in.
loglog(ax, x, y)
ax.XRuler.TickLabel
Warning: Negative data ignored
ans = 5x1 cell array
{'10^{-3}'} {'10^{-2}'} {'10^{-1}'} {'10^{0}' } {'10^{1}' }
ax.XRuler.TickLabelFormat
ans = '%g'
The only XRuler properties I look at that appears to control this automatic writing-in of formatted ticks is Scale
loglog(ax, x, y)
ax.XRuler.Scale = 'linear';
ax.XRuler.TickLabel
Warning: Negative data ignored
ans = 6x1 cell array
{'0'} {'1'} {'2'} {'3'} {'4'} {'5'}
My initial guess was that TickLabelFormat would control it, but it does not -- and I realized that there is no % format specifier for raised exponent so TickLabelFormat could not control it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

제품

태그

질문:

Tom
2012년 1월 28일

댓글:

2021년 2월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by