Take the 2-minute tour ×

Stupid, simple question - is the value of gcf in matlab always going to be the figure number of the active figure? I.e., if I'm working on Figure 5, will gcf always return 5?

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

GCF returns the handle of the "current figure". This is always the figure number of the active figure. However, if you click on a different figure in the meantime, that other figure will become active. Thus, if you already know what figure you're working with, because you either forced the handle to 5 by calling figure(5), or because you captured the handle in a variable by calling fh=figure; it is safer that you use the handle instead of gcf whenever you want to modify the figure to avoid risking to inadvertently making another figure active.

Also, if there is no figure currently open, gcf will open a new figure.

share|improve this answer
@yoda - thanks, my main question was what Jonas said in his second sentence - is it always true that current handle === figure number of active figure. Thanks! –  eykanal May 26 '11 at 14:28
add comment

This is a little more complicated than a simple "yes" or "no" answer. The handle for the current figure will generally match the number displayed at the top left of the figure window, but this number is only displayed when the 'NumberTitle' figure property is set to 'on' (the default).

Another wrinkle is that the figure handle is not guaranteed to be an integer. There is an 'IntegerHandle' figure property which determines if the handle created for the figure is an integer or a non-reusable real number. If this property is set to 'off', you get handle values that aren't integers, so the first figure that you open won't have a handle of 1. For example:

>> hFigure = figure('IntegerHandle','off')  %# The only window open

hFigure =


And the figure is numbered accordingly:

enter image description here

Notice that when the figure number and handle are displayed, there is some round-off of the number. The figure window only displays 6 digits past the decimal place. It becomes apparent that you're dealing with floating point numbers when you change the format of the Command Window to show more decimal places:

>> format long
>> hFigure

hFigure =


In this case, the displayed figure number and the figure handle differ slightly.

share|improve this answer
what would be a good reason to set 'IntegerHandle' to 'off'? –  yoda May 26 '11 at 17:47
@yoda: My guess is that it helps insulate your figures from anything the user might do. For example, if you create a GUI with a figure handle of 1, and the user runs some other code while the GUI is open that does something like set(1,...); (i.e. they hardcode it to modify the first open figure), then they will probably break your GUI. Using non-integer handles makes your GUI figure handle unique, and the handle numbers aren't recycled like integer handles are. –  gnovice May 26 '11 at 18:01
You have a point there. Although the algorithm that generates the handles is very deterministic and handles across sessions can be identical, within a session it is not recycled. –  yoda May 26 '11 at 18:13
From the docs: IntegerHandle: If you set this property to off, MATLAB assigns nonreusable real-number handles (for example, 67.0001221) instead of integers. This feature is designed for dialog boxes where removing the handle from integer values reduces the likelihood of inadvertently drawing into the dialog box. –  sebastian Sep 26 '13 at 15:15
add comment

Yes, gcf will return the handle of the currently selected (or active) figure. From the documentation,

H = GCF returns the handle of the current figure. The current figure is the window into which graphics commands like PLOT, TITLE, SURF, etc. will draw.

But also remember that:

The current figure is not necessarily the frontmost figure on the screen.

One way to make a figure "current" is:

Clicking on uimenus and uicontrols contained within a figure, or clicking on the drawing area of a figure cause that figure to become current.

Another way is to use the figure handle. i.e., if you called the figure as h=figure;, then figure(h) will make it the current figure.

share|improve this answer
add comment

Your Answer


