MATLAB Answers

Inconsistent behavior updating tick labeling

4 views (last 30 days)
Leo Simon
Leo Simon on 13 Feb 2018
Answered: Steven Lord on 13 Feb 2018
In the code below, I'm adding ticks to the Y axis. The first time I do it, YTickLabel updates correctly, adding an additional label. The second time I do it, YTickLabel fails to update properly, keeping the same number of elements when it should add one. I've played around with examples. In this case, the problem appears to be attributable to the fact that I'm adding a new tick below the first tick. If the second line of code is uncommented, then updating happens correctly. Is this a bug? Is there a workaround?
plot(0.5 :0.5:4.6)
%plot(0.0 :0.5:4.6)
YTick = get(gca,'YTick');
set(gca,'YTick',unique([ YTick , 1.9827 ]));
YTick = get(gca,'YTick');
YTickLabel = get(gca,'YTickLabel');
YTick
sprintf('YTick has %i elements', numel(YTick))
YTickLabel'
pause
set(gca,'YTick',unique([ YTick , 0.3519 ]));
YTick = get(gca,'YTick');
YTickLabel = get(gca,'YTickLabel');
YTick
sprintf('YTick has %i elements', numel(YTick))
YTickLabel'
pause

Accepted Answer

Steven Lord
Steven Lord on 13 Feb 2018
If the axes property YTickLabelMode is set to 'auto' (which it is by default) and one of the values of the property YTick is outside the limits of the visible area of the axes returned by ylim, does the YTickLabel property contain a label for that non-visible tick? It does not. If you want that tick to be labeled, you can:
  1. explicitly set the YTickLabel property of the axes (directly or using the yticklabels function, if you're using release R2016b or later) which will change the YTickLabelMode as a side effect or
  2. change the limits of the axes using ylim so that all the ticks whose labels you want to see are visible

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!