Close all figures which are not docked

2 visualizaciones (últimos 30 días)
David H
David H el 25 de Nov. de 2019
Respondida: Rik el 25 de Nov. de 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.

Respuesta aceptada

Constantino Carlos Reyes-Aldasoro
Constantino Carlos Reyes-Aldasoro el 25 de Nov. de 2019
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 comentarios
Rik
Rik el 25 de Nov. de 2019
Some methods of maximizing figures use a similar method, so this check is not fool-proof.
David H
David H el 25 de Nov. de 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!

Iniciar sesión para comentar.

Más respuestas (1)

Rik
Rik el 25 de Nov. de 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

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by