필터 지우기
필터 지우기

accessing subplot grid title (sgtitle) from figure properties

조회 수: 49 (최근 30일)
hxen
hxen 2023년 6월 14일
편집: hxen 2023년 6월 14일
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.

채택된 답변

Star Strider
Star Strider 2023년 6월 14일
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
Kids =
3×1 graphics array: Text (Subplots) Axes (Subplot #2) Axes (Subplot #1)
obj = findobj(gcf)
obj =
6×1 graphics array: Figure (1) Text (Subplots) Axes (Subplot #2) Axes (Subplot #1) Line Line
sgt = findobj(hf, 'Type','Text')
sgt =
0×0 empty GraphicsPlaceholder array.
sgt = findobj(hf.Children, 'Type','Text')
sgt =
0×0 empty GraphicsPlaceholder array.
sgt = Kids(1)
sgt =
Text (Subplots) with properties: String: 'Subplots' FontSize: 13 FontWeight: 'normal' FontName: 'Helvetica' Color: [0 0 0] Interpreter: 'tex' Show all properties
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
hxen
hxen 2023년 6월 14일
Thanks Strider. I could find no documentation and you showed me a neat way to hunt for propoerties not readily shown.
Star Strider
Star Strider 2023년 6월 14일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2023년 6월 14일
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
ans = 'subplottext'
hf = gcf;
sgt = findobj(hf, 'Type','subplottext')
sgt =
Text (Subplots) with properties: String: 'Subplots' FontSize: 13 FontWeight: 'normal' FontName: 'Helvetica' Color: [0 0 0] Interpreter: 'tex' Show all properties
sgt == h % true
ans = logical
1
You can see the list of properties of that object in the documentation.
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.
  댓글 수: 4
Star Strider
Star Strider 2023년 6월 14일
Thank you!
I don’t usually look at the Identifiers section of the documentation. I’ll start.
hxen
hxen 2023년 6월 14일
편집: hxen 2023년 6월 14일
You guys are awesome and love this community who are taking their time out to share their experience and knowledge. I've learned a lot with a recent needed analysis project from things I thought I new (like data precission handling) to arguably more esoteric points. The feedback I've gotten here has been very helpful! Cheers.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by