accessing subplot grid title (sgtitle) from figure properties
47 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
How to get the sgtitle of a figure directly from the properties?
For the fig title, this is fairly straightforward and ideal when wishing to save a large volume of open figures automatically:
titleName = gca(figObj).Title.String
However there is no obvious property name for subplot grid title (sgtitle) that can be readily accessed. Yet clearly the sgtitle information is retained after closing as it reappears upon reopening a figure. Is it just that this property is not accessible to users? This seems unlikely, especially given the many dozens of properties available.
Cheers.
0 commentaires
Réponse acceptée
Star Strider
le 14 Juin 2023
The sgtitle object is a child of the figure. The problem is that it doesn’t appear straightforward to access using findobj.
x = [1;1]*(0:15);
y = randn(2, size(x,2));
figure
subplot(2,1,1)
plot(x(1,:), y(1,:))
xlabel('x_1')
ylabel('y_1')
title('Subplot #1')
subplot(2,1,2)
plot(x(2,:), y(2,:))
xlabel('x_2')
ylabel('y_2')
title('Subplot #2')
sgtitle('Subplots')
hf = gcf;
Kids = hf.Children
obj = findobj(gcf)
sgt = findobj(hf, 'Type','Text')
sgt = findobj(hf.Children, 'Type','Text')
sgt = Kids(1)
It’s definitely possible to get it, however its type appears not to correspond with what I’d normally expect. That appears to be some undocumented property.
.
2 commentaires
Plus de réponses (1)
Steven Lord
le 14 Juin 2023
The object created and returned by sgtitle has a Type that is not the same as its class.
x = [1;1]*(0:15);
y = randn(2, size(x,2));
figure
subplot(2,1,1)
plot(x(1,:), y(1,:))
xlabel('x_1')
ylabel('y_1')
title('Subplot #1')
subplot(2,1,2)
plot(x(2,:), y(2,:))
xlabel('x_2')
ylabel('y_2')
title('Subplot #2')
h = sgtitle('Subplots');
h.Type
hf = gcf;
sgt = findobj(hf, 'Type','subplottext')
sgt == h % true
Of course, even easier is to call sgtitle with an output argument and keep that variable around, as I did with the variable named h.
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!