XLabel and YLabel Font Size
Show older comments
I'm currently trying to create a script that can automatically format all of my plots for me. I'm encountering a weird issue in trying to set the font size for the XLabel and YLabel.
To demonstrate this problem.
s = tf('s');
H = 1/(s+1); %some function
step(H); %creating a plot
%seeing the properties of XLabel
h = xlabel("Time", "FontSize", 20);
h =
Text (time (seconds)) with properties:
String: 'Time'
FontSize: 20
FontWeight: 'normal'
FontName: 'Helvetica'
Color: [0 0 0]
HorizontalAlignment: 'center'
Position: [223 -20.6667 0]
Units: 'pixels'
So far, everything works and we can print out h and see that it has the correct font size.
However, if we look at the font size property using gca, it doesn't work.
ax = gca;
ax.XLabel
ans =
Text with properties:
String: ''
FontSize: 11
FontWeight: 'normal'
FontName: 'Helvetica'
Color: [0.4000 0.4000 0.4000]
HorizontalAlignment: 'center'
Position: [4.5000 -0.0744 -10]
Units: 'data'
Then, if we use gca to set the XLabel, another label pops up. It seems like there are two separate XLabel properties within the plot.
ax.XLabel.String = "hello";

I tried to replicate this problem with plot(), but that function works fine. Maybe this is unique to step(). I haven't tested with other plotting functions. Any advice on what to do if I want to automate formatting all my plots using gca?
3 Comments
Adam Danz
on 17 Nov 2020
Notice that the two text objects you shared aren't the same xlabel (copied below).
They have different String and position properties. I doubt they are referring to the same xlabel.
Text (time (seconds)) with properties:
String: 'Time'
FontSize: 20
FontWeight: 'normal'
FontName: 'Helvetica'
Color: [0 0 0]
HorizontalAlignment: 'center'
Position: [223 -20.6667 0]
Units: 'pixels'
Text with properties:
String: ''
FontSize: 11
FontWeight: 'normal'
FontName: 'Helvetica'
Color: [0.4000 0.4000 0.4000]
HorizontalAlignment: 'center'
Position: [4.5000 -0.0744 -10]
Units: 'data'
Thanh Nguyen
on 17 Nov 2020
Accepted Answer
More Answers (1)
You can access the axis labels in the step function directly with 2-3 lines of code.
The xlabel and the ax.XLabel return different values which tells you that for whatever reason, the step() function has more than 1 pair of axes. So you need to find the axes handle that contains the displayed xlabel and that's the handle with the "Step Response" title.
Here's how to get the correct axes handle containing the displayed title and change the xlabel fontsize:
% Generate the step plot
s = tf('s');
H = 1/(s+1); %some function
step(H);
% Get the correct axes handle (ax)
axs = findall(gcf,'Type','Axes'); % call immediately after producing figure
titles = axs.Title;
ax = axs(arrayfun(@(t)strcmpi(t.String, 'Step Response'),[axs.Title]));
% Change xlabel fontsize
ax.XLabel.FontSize = 20;
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

