Close all figures which are not docked

조회 수: 3 (최근 30일)
David H
David H 2019년 11월 25일
답변: Rik 2019년 11월 25일
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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rik
Rik 2019년 11월 25일
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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by