How do I change 1e-5 on a loglog axis out of scientific notation?

6 views (last 30 days)
I would like to whole y-axis label on my loglog plot to be in standard notation not scientific. For example:
x=loglog([10, 10000], [.00005, .5])
ax=gca
ax.YLim = [0.000001 1];
ax.YTickLabel = ax.YTick;
I've tried a number of methods and looked on a number of forums but no matter what I do the1e-05 and below will not change. Even when you manually change it in the property editor it defaults back to scientific.
Is there a system property that is defaulting this?
The code example above is simplified but illustrates the point.

Accepted Answer

Les Beckham
Les Beckham on 26 Jun 2025
One possibility:
x=loglog([10, 10000], [.00005, .5]);
grid on
ax=gca;
ax.YLim = [0.000001 1];
ax.YTickLabel = compose('%f', ax.YTick);
  7 Comments
Benjamin Kraus
Benjamin Kraus on 27 Jun 2025
It looks like @Thomas and @Les Beckham figured out a solution, but I just wanted to chime in with some additional notes (and confirmations):
  • @Walter Roberson: As you observed, the Exponent has no effect when in log-scale. This is in the documentation, but it is quite easy to miss, as it is strapped onto the end of another sentence at the end of the property description. "If the axis has a log scale, then the Exponent property has no effect."
  • The value of the YTickLabel property is always going to be either a character matrix or a cell-array. If you set the value to be a numeric vector, then the value is immediately converted into a cell-array using an internal algorithm that cannot be customized and is not impacted by any other settings in MATLAB. Ultimately, the internal code is calling the C++ function snprintf.
  • If you want to specify how the numbers are converted into strings, then you need to do the conversion yourself, as you did above using either compose, sprintf, or num2str. Each of those commands have their own unique behaviors. I believe num2str is a very similar algorithm to what is used by the YTickLabel property, but I'm not certain of that.
  • I couldn't find a solution that automatically trimmed the trailing zeros without requiring some manual intervention (such as using strip, as you did above).
One challenge MATLAB faces when automatically trimming trailing zeros is that ultimately none of the numbers in your YTick vector (except 1) are perfectly representable using floating point numbers. You can see this by printing each value to 30 decimal places and noticing that there is some inaccuracy in each case that is inherant to the way MATLAB (and basically every computer) stores numbers.
ticks = logspace(-6,0,7)';
compose('%0.30f',ticks)
ans = 7×1 cell array
{'0.000000999999999999999954748112'} {'0.000009999999999999999123964645'} {'0.000100000000000000004792173602'} {'0.001000000000000000020816681712'} {'0.010000000000000000208166817117'} {'0.100000000000000005551115123126'} {'1.000000000000000000000000000000'}
Unless you specify a specific number of significant digits, MATLAB needs to make a judgement call as to how much precision to show. In some circumstances "0.1" an d"0.1000" are interpretted differently, as the later implies that the value is accurate to 4 decimal places while the former does not.
That being said, it sounds (to me) like a reasonable enhancement to compose to support automatically truncating trailing zeros (and trailing decimal separator). I'll relay that feedback to the appropriate development team.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!