2

I have found that under some circumstances MATLAB will print negative -0.0 as an axis tick. How can I eliminate this?

Problem code

y = linspace(-.4,.3,100);
plot(y)
ax = gca();
ax.YAxis.TickLabelFormat = '%.1f'`

This code produces a plot like this where instead of zero tick reading '0.0' it reads '-0.0'.

I think the key here is that the interval between the maximum and minimum of the Y axis cannot be represented exactly as a float, so the 'zero' tick is actually located at -5.6e-17. You can see this if instead use the formatting ax.YAxis.TickLabelFormat = '%.1e'

For picky formatting reason, I really prefer that all my axis ticks have the same number of digits (i.e. -0.1, 0.0, 0.1 rather than -0.1, 0, 0.1). Is there any way to eliminate the negative zero in this formatting?

Using MATLAB2017a on MacOS Sierra 10.12.4

Community
  • 1
  • 1

1 Answers1

1

You choose floating point format, you get what you wanted with floating point accuracy issues. You can define y = linspace(-.4,.3,100)+eps; to solve it...

Community
  • 1
  • 1
bla
  • 25,846
  • 10
  • 70
  • 101
  • Thanks, this works! In the case of my actual problem (which involved using the `axis` command), it can similarly be solved by changing, for example, `axis([0 100 -0.4 0.3])` to `axis([0 100 -0.4 0.3+eps])` – taciteloquence May 10 '17 at 16:51
  • Also I've submitted a bug report to Mathworks for this issue and the workaround. – taciteloquence May 10 '17 at 17:02