close - Close one or more figures - MATLAB (original) (raw)
Close one or more figures
Syntax
Description
close
closes the current figure. Calling close
is equivalent to calling close(gcf)
.
close([fig](#mw%5F2029b5d0-2c51-4710-a1bb-0699417d22ea))
closes the figure specified byfig
.
close all
closes all figures whose handles are visible. A figure handle is hidden if the HandleVisibility property is set to 'callback'
or 'off'
.
close all hidden
closes all figures, including figures with hidden handles.
close all force
closes all figures, including figures for which theCloseRequestFcn callback has been specified to prevent users from closing the figure window.
status = close(___)
returns thestatus
of the close operation for any of the previous syntaxes. The function returns 1
if the figure or figures close and0
otherwise. When specifying the output status
, you must enclose input arguments that are character vectors in parentheses; for example,status = close('all','hidden')
.
Examples
Close a Single Figure
Create two figures, each with a line plot.
f2 = figure; plot((1:10).^2)
Close the first figure and display the value of f1
.
f1 = handle to deleted Figure
Close the current figure.
Close Multiple Figures
Create three figures, and then create a line plot. By default, the plot
function targets the current figure (f3
).
f1 = figure; f2 = figure; f3 = figure; plot(1:10)
Close figures f1
and f2
simultaneously.
Close Figure with Specified Number
Create two figures with specified numbers. Include a line plot in each figure.
figure(2) plot((1:10).^2)
Close the second figure by passing its number to the close
function.
Close Figure with Specified Name
Create a figure, specify its name, and then create a line plot.
figure('Name','Measured Data'); plot(1:10)
Close the figure using its name.
Verify Figure Is Closed
Create two figures, each with a line plot.
f2 = figure; plot((1:10).^2)
Close figure f1
. Verify that the figure is closed by displaying its status.
Close All Figures with Visible Handles
Create three figures whose handles are visible, and include a line plot in each figure.
f2 = figure; plot((1:10).^2)
f3 = figure; plot(1./(1:10))
Close all of the figures simultaneously.
Close All Figures with Visible or Hidden Handles
Create three figures, each with a line plot. Set the HandleVisibility
property of the last figure to 'off'
.
f2 = figure; plot((1:10).^2)
f3 = figure('HandleVisibility','off'); plot(1./(1:10))
Close all of the figures. Notice that you cannot close f3
by calling close all
because it has a hidden handle.
Force Figure to Close
The CloseRequestFcn
property enables you to specify a close request callback, which executes whenever a user attempts to close the figure window. For example, you can display a dialog box asking to confirm or cancel the close operation or to prevent users from closing a figure that contains a UI.
Create a figure whose window cannot be closed by setting the CloseRequestFcn
property to an empty character vector. Then, add a line plot to the figure.
f1 = figure('CloseRequestFcn',''); plot(1:10)
Create a second figure with a line plot.
f2 = figure; plot((1:10).^2)
If you try to close the figures using the close all
syntax, MATLAB® closes only f2
. To close both f1
and f2
, use the close all force
syntax.
Input Arguments
fig
— Figure to close
one or more Figure
objects, figure numbers, or figure names
Figure to close, specified as one or more Figure
objects, figure numbers, or figure names.
- If
fig
is a figure number, MATLAB® searches for an existing figure in which the Number property is equal tofig
. By default, theNumber
property value is displayed in the title of the figure. - If
fig
is a figure name, MATLAB searches for an existing figure in which the Name property is equal tofig
.
Example: close(f)
closes the figure with handlef
.
Example: close([f1 f2])
closes the figures with handlesf1
and f2
.
Example: close(1)
closes the figure with number1
.
Example: close([1 2])
closes the figures with numbers1
and 2
.
Example: close('My Figure')
closes the figure with name'My Figure'
.
Example: close('My First Figure','My Second Figure')
closes the figures with names 'My First Figure'
and 'My Second Figure'
.
Tips
- To delete all figures unconditionally, use these statements:
set(groot,'ShowHiddenHandles','on')
c = get(groot,'Children');
delete(c) - When implementing a
CloseRequestFcn
callback, do not use a call toclose
. Callingclose
in the body of the callback sets up a recursion that results in a MATLAB warning. Instead, implement the callback using thedelete
function.delete
removes the figure without executing theCloseRequestFcn
callback. - If you call
close
on a figure without specifying theCloseRequestFcn
property, the default value of the property,closereq
, unconditionally deletes the figure and closes its window. To prevent deletion when callingclose
, implement aCloseRequestFcn
callback.
Algorithms
The close
function evaluates the CloseRequestFcn
property of the specified figure f
using this statement:
eval(get(f,'CloseRequestFcn'))
CloseRequestFcn
enables you to either delay or abort the closing of a figure once close
has been invoked. For example, you can display a dialog box to confirm that the user really wants to close the figure or save and clean up before closing.
The default value of CloseRequestFcn
, closereq
, closes the current figure using delete(get(groot,'CurrentFigure'))
. If you specify an array of figure handles, close
executes the callback specified by CloseRequestFcn
for each figure.
If an error terminates the execution of a CloseRequestFcn
callback, then the figure is not closed.
Version History
Introduced before R2006a