MATLAB Answers


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

Asked by Tom
on 28 Jan 2012
Latest activity Commented on by Walter Roberson
on 9 May 2018
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);
%set(gca,'XTick', [0,1,10,100,1000,10000]); %set(gca,'XTickLabel',sprintf('%3.4f|',f))


Sign in to comment.



1 Answer

Answer by Walter Roberson
on 28 Jan 2012
 Accepted Answer

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.


Show 1 older comment
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.)

Sign in to comment.