Reputation: 27077
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
?
Upvotes: 6
Views: 5706
Reputation: 125874
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 =
173.0040
And the figure is numbered accordingly:
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 =
1.730040283203125e+002
In this case, the displayed figure number and the figure handle differ slightly.
Upvotes: 5
Reputation: 42245
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.
Upvotes: 3
Reputation: 74940
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.
Upvotes: 8