XLabel and YLabel Font Size
29 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Thanh Nguyen
am 17 Nov. 2020
Kommentiert: Adam Danz
am 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 Kommentare
Akzeptierte Antwort
Star Strider
am 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 Kommentare
Cris LaPierre
am 17 Nov. 2020
Bearbeitet: Cris LaPierre
am 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(2)
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
am 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.
Weitere Antworten (1)
Adam Danz
am 17 Nov. 2020
Bearbeitet: Adam Danz
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!