Close all figures which are not docked

3 Ansichten (letzte 30 Tage)
David H
David H am 25 Nov. 2019
Beantwortet: Rik am 25 Nov. 2019
Is there any easy way to close all figures which are not docked? Either a high level command or a way to loop over figures and check if it is docked and close it iff it is not.

Akzeptierte Antwort

Constantino Carlos Reyes-Aldasoro
When figures are docked, the position always start in the origin (i.e. 1,1), you could detect this location by using gcf like this:
>> get(gcf,'Position')
ans =
1 1 623 237
In contrast, when you open a figure, it will not be in the origin:
>> get(gcf,'Position')
ans =
560 528 560 420
Thus, if you loop through your figures and check the origin, you can select those which have 1,1 and close those ones, and leave the other ones open (or vice versa).
  2 Kommentare
Rik
Rik am 25 Nov. 2019
Some methods of maximizing figures use a similar method, so this check is not fool-proof.
David H
David H am 25 Nov. 2019
I have found sometimes the position of the docked figures can be [0 0 x y] instead of [1 1 x y], as such I made the function like this:
function close_all_undocked
figHandles = findobj('Type', 'figure');
positions = cat(1,figHandles.Position);
% Determine if figures are docked by position (default 1 1 or 0 0)
isdocked = positions(:,1) == 1 | positions(:,1)== 0;
isdocked = isdocked & (positions(:,2) == 1 | positions(:,2) == 0);
% Close selected figures
figToClose = figHandles(~isdocked);
close(figToClose, 'Force')
end
This seems to work ok, but I guess it might need some testing to make sure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Rik
Rik am 25 Nov. 2019
Using the script below I generate the list of properties that are different between docked and undocked figures. After running this, the variable s_diff contains the comparison. Using this I found the WindowState property, which was already present in relatively old releases ( doclink to R2012b ). So you can use this property to find and close all undocked figures:
function close_all_undocked
figHandles = findobj('Type','Figure','-not','WindowStyle','docked');
close(figHandles,'Force')
end
Although in this case it would have been faster to search the documentation for useful properties, this shows the method for when that fails.
try close(1),catch,end
try close(2),catch,end
f1=figure(1);clf(1)
f2=figure(2);clf(2)
uiwait(msgbox('dock one figure and hit ok to continue'))
s1=struct(f1);
s2=struct(f2);
drawnow
clc % clear the warning about unhiding implementation details
fn1=fieldnames(s1);
fn2=fieldnames(s2);
if ~isequal(fn1,fn2)
error('fields don''t match!')
end
s_diff=struct;
for n=1:numel(fn1)
if ~isequal(s1.(fn1{n}),s2.(fn1{n}))
s_diff(1).(fn1{n})=s1.(fn1{n});
s_diff(2).(fn1{n})=s2.(fn1{n});
end
end

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by