How to clear (not close) all the opened figures?

48 Ansichten (letzte 30 Tage)
Giovanni Franzini
Giovanni Franzini am 3 Sep. 2019
Kommentiert: Walter Roberson am 10 Aug. 2025
I would like to ask what is the easiest way to clear all the figures (not closing them) already opened.
The "clf" command is useful, but it clears only the current figure.
Is there a command or some easy instruction to clear all figures, such as "close all" used for closing them?
  1 Kommentar
Stephen23
Stephen23 am 3 Sep. 2019
The shotgun approach is a bad way to write code. Avoid doing this.
"I would like to ask what is the easiest way to clear all the figures"
The simplest solution is to write more robust code: put all of the figure handles into one array, then looping over them is trivially easy and very efficient.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 3 Sep. 2019
Bearbeitet: Jan am 3 Sep. 2019
function clfall
FigList = findall(groot, 'Type', 'figure');
for iFig = 1:numel(FigList)
try
clf(FigList(iFig));
catch
% Nothing to do
end
end
end
As Stephen said already: This is a shotgun method. I would not use it in productive code, because this will kill other GUIs.
I'd prefer to insert a button or menu for clearing:
function myFigure(varargin)
FigH = figure(varargin{:});
setappdata(FigH, 'myTag', 'myFigureGroup');
uicontrol('Style', 'PuchButton', 'String', 'clear', ...
'Tooltipstring', 'Celar all figures of this group.', ...
'Units', 'pixels', 'Position', [2, 2, 40,20], ...
'Callback' @myFigureClear);
end
function myFigureClear(FigH, EventData)
FigList = findall(groot, 'Type', 'figure');
for iFig = 1:numel(FigList)
FigH = FigList(iFig);
Tag = getappdata(FigH, 'myTag');
if strcmp(Tag, 'myFigureGroup')
clf(FigH);
end
end
end
Now create new figures, which should be affected by the clearing, with myFigure instead of calling figure. Then an additional button is created in the left bottom, which clears the corresponding figures, but only if the myTag field in the ApplicationData is matching.
By the way, the "my" is for demonstration only. If everybody call his/her personal functions "my...", there will be collisions also. Prefer "gioFra" instead in the tags. "figure2" will be used frequently also, while I have not seen "figureX" yet.
  1 Kommentar
Walter Roberson
Walter Roberson am 10 Aug. 2025
With less protection:
arrayfun(@clf, mfindall(groot, 'Type', 'figure'), 'uniform', 0);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Masoud Aliramezani
Masoud Aliramezani am 19 Feb. 2020
Quite surprising that MATLAB doesn't have something like "clf all". Perhaps that's something to look at for the next updates.
  2 Kommentare
Duncan
Duncan am 8 Aug. 2025
close all works
DGM
DGM am 9 Aug. 2025
The question was about clearing, and explicitly not about closing.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by