XTicks change in tiled figure if no breakpoint is set before for loop, even if xticks are used

Hello everyone,
I am plotting several figures within a for loop, each with a tile layout of 3x3. The x-axis is plotted over 0 to 100%. As the default font size is too small, I have made it larger.
Unfortunately, this operation changes the x-axis ticks from the desired [0 20 40 60 80 100] to [0 50 100 0 50 100], even if I also adjust the x-ticks, as can be seen below.
This effect does not occur if I set a breakpoint directly before the for loop, e.g. in the line ticks = ...
The function consists of 440 lines in which several calculations are performed before the figure is created.
Processing the script with the breaktpoint is not an option for me.
I have already tried changing the font size in various places. Everything I have tried seems to lead to this problem.
Do you have any idea how to solve this problem? Is it perhaps a question of the settings?
Thank you :)
This is without the breakpoint:
This is when I do place the breakpoint in e.g. the line with ticks = 0:20:100; (This is what I want it to result in):
figure(2)
tiledlayout(3, 3);
percent = 0:100;
ticks = 0:20:100;
for i = 1:9
nexttile(i)
a = randi(5, 101, 1);
plot(percent, a)
h = get(gca,'XTickLabel');
set(gca,'XTickLabel',h,'fontsize',15)
xticks(ticks)
end

 Accepted Answer

I can't explain why your approach didn't work, but this seems to work.
figure
tiledlayout(3, 3);
percent = 0:100;
ticks = 0:20:100;
for i = 1:9
nexttile(i)
a = randi(5, 101, 1);
plot(percent, a)
% h = get(gca,'XTickLabel');
% set(gca,'XTickLabel',h,'fontsize',15)
ax = gca;
ax.XAxis.FontSize = 15;
xticks(ticks)
end

4 Comments

Thank you for testing!
Do you think it could be due to the number of operations I perform beforehand? For me it also works when I process only the given code in a seperate script.
@Jana: The reason this happens is that the line
set(gca,'XTickLabel',h,'fontsize',15)
sets the XTickLabel of the axes, which has the side effect of also setting the XTickLabelMode to 'manual', which means that the XTickLabel does not update when xticks() is called (only the ticks change), so the XTickLabel remains {'0','50','100'} and it repeats to fill all the xticks. [I didn't find any difference in behavior with or without the breakpoint.]
The solution @Les Beckham shows avoids setting the XTickLabel (and thus avoids setting the XTickLabelMode inadvertently), and sets the FontSize only, which is what is intended.
Thank you @Les Beckham and @Voss. I have tested the approach and it works within the function for both the x-axis and the y-axis using the suggested approach.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 10 Nov 2023

Commented:

on 10 Nov 2023

Community Treasure Hunt

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

Start Hunting!