Access All Figure Handles Using groot Function in MATLAB

Aug. 04, 2025 • Updated Aug. 05, 2025

In MATLAB, we can use gcf1 to get the current figure handle, which way is commonly used. To step further, we can access all figure handles with the help of groot function2. Take the case of three figures as an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
clc, clear, close all
rng("default")

num = 100;
x = 1:num;
y1 = randn(1, num);
y2 = randn(1, num);
y3 = randn(1, num);

figure("Name", "fig1")
plot(x, y1)

figure("Name", "fig2")
plot(x, y2)

figure("Name", "fig3")
plot(x, y3)

r = groot

then we’ll have variable r:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
r = 
  Graphics Root with properties:

          CurrentFigure: [1×1 Figure]
    ScreenPixelsPerInch: 96
             ScreenSize: [1 1 2560 1440]
       MonitorPositions: [2×4 double]
                  Units: 'pixels'

  Show all properties

         CallbackObject: [0×0 GraphicsPlaceholder]
               Children: [3×1 Figure]
          CurrentFigure: [1×1 Figure]
     FixedWidthFontName: 'Courier New'
       HandleVisibility: 'on'
       MonitorPositions: [2×4 double]
                 Parent: [0×0 GraphicsPlaceholder]
        PointerLocation: [544.3333 629]
            ScreenDepth: 32
    ScreenPixelsPerInch: 96
             ScreenSize: [1 1 2560 1440]
      ShowHiddenHandles: off
                    Tag: ''
                   Type: 'root'
                  Units: 'pixels'
               UserData: []

and among which r.Children is:

1
2
3
4
5
6
ans = 
  3×1 Figure array:

  Figure    (3: fig3)
  Figure    (2: fig2)
  Figure    (1: fig1)

and

1
r.Children(1), r.Children(2), r.Children(3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ans = 
  Figure (3: fig3) with properties:
      Number: 3
        Name: 'fig3'
       Color: [0.9400 0.9400 0.9400]
    Position: [1000 817.6667 560 420.0000]
       Units: 'pixels'
  Show all properties

ans = 
  Figure (2: fig2) with properties:
      Number: 2
        Name: 'fig2'
       Color: [0.9400 0.9400 0.9400]
    Position: [1000 817.6667 560 420.0000]
       Units: 'pixels'
  Show all properties

ans = 
  Figure (1: fig1) with properties:
      Number: 1
        Name: 'fig1'
       Color: [0.9400 0.9400 0.9400]
    Position: [1000 817.6667 560 420.0000]
       Units: 'pixels'
  Show all properties

So, we can save these three figures easily by:

1
2
3
exportgraphics(r.Children(3), "fig1.png", "Resolution", 600)
exportgraphics(r.Children(2), "fig2.png", "Resolution", 600)
exportgraphics(r.Children(1), "fig3.png", "Resolution", 600)

By the way, note the order of figures.


References