Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find an sgtitle in a figure with a tiledlayout?

I have a figure with a tiledlayout. If I create an sgtitle I am unable to find that same sgtitle within the fh.Children structure. If I do not create a tiledlayout, then the sgtitle shows up in the list of Children. How can I access an sgtitle in a figure that has a tiledlayout? Here is an example:

% Figure that can still find and access the sgtitle
fh_with_sgtitle_handle = figure;
plot(rand(100,1))
title('Just a Plot')
sgtitle('This is a test')

% Figure that doesn't show the sgtitle in the Children list
fh_wo_sgtitle_handle = figure;
tiledlayout
nexttile
plot(rand(100,1))
nexttile
plot(rand(100,1))
title('Lower plot')
sgtitle('This is a test')

% Try to find the sgtitle
fh_wo_sgtitle_handle.Children
fh_with_sgtitle_handle.Children

I guessed it would be in the Children of the tiledlayout, however:

fh_wo_sgtitle_handle.Children(1).Children

ans =

2×1 Axes array:

Axes (Lower plot)
Axes

My example is on Matlab 2024b. sgtitle shows up as a subplottext but findall can only find it in the fh_with_sgtitle_handle. Where did it go?

like image 772
Matt Avatar asked Oct 24 '25 03:10

Matt


1 Answers

When the figure has a TiledLayout, the sgtitle is a text object inside the Title property of the TiledLayout object, which is a child of the figure. In your example (I am using R2025a),

>> ch = fh_wo_sgtitle_handle.Children
ch = 
  TiledChartLayout (This is a test) with properties:

    TileArrangement: 'flow'
           GridSize: [1 2]
            Padding: 'loose'
        TileSpacing: 'loose'

  Show all properties
>> ch.Title
ans = 
  Text with properties:

         String: 'This is a test'
       FontSize: 13
     FontWeight: 'normal'
       FontName: 'Helvetica'
          Color: [0.1294 0.1294 0.1294]
    Interpreter: 'tex'

  Show all properties
>> ch.Title.String
ans =
    'This is a test'

When the figure does not have a TiledLayout, the sgtitle cannot be there. So Matlab just creates a text object as a child of the figure:

>> ch = fh_with_sgtitle_handle.Children
ch = 
  2×1 graphics array:

  Text    (This is a test)
  Axes    (Just a Plot)
>> ch(1).String
ans =
    'This is a test'
like image 82
Luis Mendo Avatar answered Oct 26 '25 02:10

Luis Mendo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!