how to grab text from figure's suptitle and subplot titles

23 views (last 30 days)
I'm familiar with how to grab the title from a simple figure such as this:
htitle = get(gca,'Title');
stitle = htitle.String
This works sufficient for a simple plot/title, but when a figure has subplots (each with a title) and a "suptitle" title on top, the function above seems to grab a title I don't want.
1) How do I specifically grab the suptitle text?
2) How do I grab the title text from say subplot(2,2,1)?
Thank you for your help!

Answers (1)

Kevin Holly
Kevin Holly on 21 Jan 2022
subplot(2,2,1)
plot(1:10,rand(1,10))
title('pick me');
subplot(2,2,2)
plot(1:10,rand(1,10))
title('not me');
subplot(2,2,3)
plot(1:10,rand(1,10))
title('not me');
subplot(2,2,4)
plot(1:10,rand(1,10))
title('not me');
sgtitle('Select me')
handle = gcf %obtain the handle of the figure (get current figure).
handle =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [671 661 577 433] Units: 'pixels' Show all properties
handle.Children %Look at the Children of the figure
ans =
5×1 graphics array: Text (Select me) Axes (not me) Axes (not me) Axes (not me) Axes (pick me)
handle.Children(1) %Select first child (the text of the sgtitle)
ans =
Text (Select me) with properties: String: 'Select me' FontSize: 13 FontWeight: 'normal' FontName: 'Helvetica' Color: [0 0 0] Interpreter: 'tex' Show all properties
handle.Children(end) %Select the last child (the axes of subplot(2,2,1)
ans =
Axes (pick me) with properties: XLim: [0 10] YLim: [0 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.5838 0.3347 0.3412] Units: 'normalized' Show all properties
handle.Children(end).Title %Now select the title
ans =
Text (pick me) with properties: String: 'pick me' FontSize: 9.9000 FontWeight: 'bold' FontName: 'Helvetica' Color: [0 0 0] HorizontalAlignment: 'center' Position: [5.0000 1.0167 1.4211e-14] Units: 'data' Show all properties
handle.Children(end).Title.String %and now the string of the title
ans = 'pick me'

Community Treasure Hunt

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

Start Hunting!