MATLAB Answers

XLabel and YLabel Font Size

27 views (last 30 days)
Thanh Nguyen
Thanh Nguyen on 17 Nov 2020
Commented: Adam Danz on 17 Nov 2020
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
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
Thanh Nguyen on 17 Nov 2020
Right, I did note that they are not the same. The question from me then is how do I acess the XLabel of the plot using gca? Maybe there is another command I'm missing?
Adam Danz
Adam Danz on 17 Nov 2020
"how do I acess the XLabel of the plot using gca? "
Explained here.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 17 Nov 2020
The Control System (and System Identification) Toolbox plot functions properties are difficult to change. That can be done, however it requires serious handle explkorations, and (in my opinion) is generally not worth the effort.
Instead, get the outputs from step and display them using plot:
s = tf('s');
H = 1/(s+1); %some function
[y,t] = step(H); %creating a plot
figure
plot(t, y)
grid
.

  4 Comments

Show 1 older comment
Star Strider
Star Strider on 17 Nov 2020
My pleasure!
EDIT — (17 Nov 2020 at 17:07)
The stepplot function offers a few more options:
s = tf('s');
H = 1/(s+1); %some function
h = stepplot(H); %creating a plot
h.AxesGrid.XLabelStyle.FontSize = 20;
h.AxesGrid.YLabelStyle.FontSize = 20;
h.AxesGrid.TitleStyle.FontSize = 20;
.
Cris LaPierre
Cris LaPierre on 17 Nov 2020
Star Strider beat me to it, but I'll add my answer as a comment here, since it includes the outputs.
The title and labels created by the step function are are hidden, so you will not be able to access them without some extra work.
s = tf('s');
H = 1/(s+1); %some function
step(H); %creating a plot
a = findall(gcf,'Type','text')
a =
6×1 Text array: Text (Step Response) Text (Time (seconds)) Text (Amplitude) Text Text Text
a(2)
ans =
Text (Time (seconds)) with properties: String: 'Time (seconds)' FontSize: 11 FontWeight: 'normal' FontName: 'Helvetica' Color: [0 0 0] HorizontalAlignment: 'center' Position: [231.9125 -20.5556 0] Units: 'pixels' Show all properties
My suggestion, then, is if you want to modify the plot, capture the outputs of the step function, then create the plot yourself.
[y,t] = step(H); %creating a plot
plot(t,y)
xlabel("Time", "FontSize", 20);
ylabel("Amplitude","FontSize", 20);
Star Strider
Star Strider on 17 Nov 2020
Cris LaPierre — Thank you!
It was likely only a few seconds difference, however your valuable contribution underscores my original observation about ‘serious handle exploration’. The derivative functions (such as stepplot for step) generally offer more options that are a bit easier to discover. Note that even stepplot required a four-deep handle structure reference to find them and set them.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 17 Nov 2020
Edited: Adam Danz on 17 Nov 2020
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;

  0 Comments

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by