Why the 'xticklabels' mismatch the 'xticks' when resizing a figure?

2 views (last 30 days)
I am plotting a figure using the following code.
x=1:1:9;
y=sin(x);
figure
plot(x,y);
Now I would like to change the 'xticks' from [0,5,10] to {'10','15','20'}.
 
set(gca,'xticklabel',{'10';'15';'20'});
Then if I resize the figure the 'xticklabels' does not match the original ticks. 
How do I resolve this issue?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Jan 2023
This is expected behavior of MATLAB when no 'xticks' is nominated and the number of assigned 'xticklabels' does not match the number of 'xticks' in a figure. If the number of 'xticklabels' is larger than the 'xticks' number (say n), then the first n 'xticklabels' will show up after rescaling the figure. If the number of 'xticklabels' is smaller than the 'xticks' number (say n), then the n 'xticklabels' will repeatedly show up after rescaling the figure.
The workaround here is to set the 'xticks' before setting the 'xticklabels' using the following code, so that the number of 'xticks' and 'xticklabels' will be the same.
set(gca,'xtick',expectedTicks);
set(gca,'xticklabel',expectedTickLabels);

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!