MATLAB Answers

How can I prevent scientific notation on my axes in MATLAB R2015a and earlier releases?

179 views (last 30 days)
Is there a way to control the output of plots so that they don't automatically display in scientific notation?
I need something that enables me to control how the tick labels are displayed on the axes. That way, when I zoom in or out, I can force the label format to non-scientific.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Jul 2019
Edited: MathWorks Support Team on 30 Jul 2019
If you are using MATLAB R2015b or a later release, please refer to the following MATLAB Answers post:
In MATLAB R2015a and earlier releases, you can use the following method.
1. Plot your data.
>> x = rand(100, 1);
>> y = 1e-3*x;
>> plot(x, y)
2. Get a handle to the current figure and axes:
>> hFig = gcf
>> hAxes = gca
3. Define a function that reformats the tick labels as follows:
function reformatTickLabels(hAxes)
XTickLabel = get(hAxes,'XTick');
set(hAxes,'XTickLabel',num2str(XTickLabel'))
YTickLabel = get(hAxes,'YTick');
set(hAxes,'YTickLabel',num2str(YTickLabel'))
end
where "hAxes" is the axes handle.
4. Call this function to format your tick labels, before zooming.
>> reformatTickLabels(hAxes);
5. Get the handle to the zoom mode object of the figure:
>> z = zoom(hFig);
7. Set the "ActionPostCallback" function on the zoom handle to the function "reformatTickLabels". This means that after you perform a zooming action, this callback function will be called and executed. In this way, the tick labels will stay in non-scientific format even after zoom events.
set(z,'ActionPostCallback',@zoomReformatLabels)
where "zoomReformatLabels" is defined as
function zoomReformatLabels(obj,evd)
reformatTickLabels(evd.Axes)
end
where "obj" is the handle to the figure that has been clicked on and "evd" is the object containing the structure of event data.
More information on the zoom callback functions can be found here:

  0 Comments

Sign in to comment.

More Answers (2)

Econstudent
Econstudent on 11 Apr 2017
Edited: Econstudent on 11 Apr 2017
It has been a while, but in case someone is looking into this, here is a solution.
1. Command your plot; 2. Use this:
Ytick = strsplit(num2str(get(gca, 'Ytick')));
This command gets the 'Ytick' values from the gca of your open plot. Then, it turns that into a single string, before splitting it into an array (which is exactly what the plot's gca uses to label the ticks).
3. The next command just makes an adjustment to the plot's gca using the new string array:
set('YtickLabel', Ytick);
The advantage here is that you avoid the use of additional objects. If you do not want to make a function (say, it's a very punctuated use), it works without crowding your workspace.

  2 Comments

Nicholas Brault
Nicholas Brault on 5 Jan 2018
Got an error when using this method:
"Error using set
Invalid handle"
Attempting to set the values on an area chart.
Steven Lord
Steven Lord on 5 Jan 2018
The code in section 3 has an error. The first input to set must be a handle to the Handle Graphics object (in this case an axes) whose properties you want to change. If you only have the handle to your area plot, use ancestor to get the 'axes' ancestor of the plot.

Sign in to comment.


Steven Lord
Steven Lord on 5 Jan 2018
Starting in release R2015b you can take advantage of rulers to customize the appearance of the axes. For example:
% Create a plot
x = linspace(0,5,1000);
y = 100*exp(x).*sin(20*x);
h = plot(x,y);
% Obtain the ruler for the Y axis of the axes
ax = ancestor(h, 'axes');
r = ax.YAxis;
% Change the format of the tick labels and remove the scientific notation
r.TickLabelFormat = '%g mm';
r.Exponent = 0;
If you were to zoom on and zoom into the plot, you will see that the Y axis labels keep the same format. If you change the tick locations the labels of the ticks keep the same format.
ax.YTick = -15000:3750:15000;
See the links in the documentation of the XAxis, YAxis, and ZAxis properties of the axes for more information about what you can do with rulers.

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2013a