How to check if the Figures window is docked and to undock it

16 次查看(过去 30 天)
Often, the Figures window is docked to the main Matlab window by default. How can I write code to undock it and, preferably, to check whether or not it is docked? Note that I'm asking about the Figures window and not about individual figure windows. For reference, I ask because I like to dock figures, e.g.
set(gcf,'windowstyle','docked')
but when sharing code, people may not be familiar with docked figures and so may not be able to find the figures.

采纳的回答

Les Beckham
Les Beckham 2025-4-1
The following two commands will undock the Figures window in 2024b and earlier:
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
desktop.setGroupDocked('Figures', false);
  5 个评论
Les Beckham
Les Beckham 2025-4-2
@Walter Roberson - That's true. Nevertheless, it is a legitimate concern that this undocumented way of controlling the docking status of the Figures window may break in future releases.
@orsijafdsoij - thanks for accepting the answer!
orsijafdsoij
orsijafdsoij 2025-4-11
Note that when I wrote a function, Matlab's IDE shows a warning saying "'com.mathworks' namespace and sub namespaces will be removed in a future release. There is no simple replacement for this." So, yeah, this is definitely NOT a permanent fix.

请先登录,再进行评论。

更多回答(2 个)

Engenuity
Engenuity 2025-3-27
Undock a current figure, use the set() function. Once all individual figures are undocked, the Figures window will be undocked (you could loop through them all)
if strcmp(get(gcf, 'windowstyle'), 'docked')
set(gcf, 'windowstyle', 'normal')
end
  6 个评论
orsijafdsoij
orsijafdsoij 2025-3-28
Yes and no. The individual figures are still docked within the Figures window, even when the Figures window is docked within the main Matlab window. That's why you can undock the entire Figures window from the main Matlab window, with all figures stored inside, with one button. But you're correct that the Figures window disappears, at least visually, if there are no open figures in it; this is true whether or not it's docked. It's also worth noting that whether the Figures window is docked is persistent, if I undock it once, then close all figures so that it disappears, the next time I create a docked figure, the Figures window will still be floating. The same is true if I close and reopen Matlab. Something is stored somewhere keeping track of this. So my goal remains the same: A programmatic way for the Figures window to not be docked inside the main Matlab window.
Engenuity
Engenuity 2025-4-1
I am not familiar with a programmatic way to dock the entire figure window.

请先登录,再进行评论。


orsijafdsoij
orsijafdsoij 2025-4-11
Following @Les Beckham's suggestion, I modified my figure-docking function with this capability. I add this here principally because, in testing, I found the behavior could be unpredictable until I introduced a pause statement. Details are in the code comments. Note also that Matlab's IDE shows a warning saying "'com.mathworks' namespace and sub namespaces will be removed in a future release. There is no simple replacement for this." So, yeah, this is definitely NOT a permanent fix.
function dockit(fig,dockUndock,dockUndockFigures)
%Function to dock/undock one or more figure windows. Can also dock/undock the Figures window.
%
% INPUTS
%fig - figure object or array of figure objects to operate on. If dockit is called without any
% inputs, the current figure window is assumed. If fig is empty, no action is taken.
%
%dockUndock - whether to dock or undock figure windows specified in fig.
% 'docked' - dock figure
% 'normal' - undock figure
% empty - do nothing
%
% dockUndockFigures - whether to dock or undock the Figures window.
% false - undock Figures window
% true - dock Figures window
% empty - do nothing
%
% NOTES
%I've found behavior can be a lil unpredictable, but hopefully have fixed it with a pause statement.
%Specifically, it seems like if you have some undocked figures, and you try to dock them and undock
%the Figures window at the same time, it may lead to the Figures window staying docked. Try running
%this sequence of commands, preferably one-at-a-time. You will need to comment out the pause line in
%this function to see the wonky behavior I was seeing.
% desktop=com.mathworks.mde.desk.MLDesktop.getInstance;
% desktop.isGroupDocked('Figures') %returns 1 if Figures window is docked, 0 if not
% a=figure;
% b=figure;
% dockit([a,b],'docked',false), desktop.isGroupDocked('Figures') %dock figures and undock Figures window
% dockit([a,b],'normal'), desktop.isGroupDocked('Figures') %undock figures
% dockit([a,b],'docked',false), desktop.isGroupDocked('Figures') %repeat
% dockit([a,b],'normal'), desktop.isGroupDocked('Figures') %repeat
%
%I think the problem ONLY happens if you do both actions in one command, if you did two sequential
%ones, it should be fine. Though, again, with the pause statement, none of this should be an issue
%anyway.
%Set default inputs
if nargin<1
fig=get(groot,'CurrentFigure'); %use current figure
end
if nargin<2
dockUndock='docked'; %dock figures
end
if nargin<3
dockUndockFigures=true(0); %empty logical, telling the code to do nothing
end
%Dock/undock figure window(s)
if ~isempty(fig)
if ~isempty(dockUndock)
set(fig,'WindowStyle',dockUndock)
end
end
%Dock/undock Figures window
if ~ismissing(dockUndockFigures)
desktop=com.mathworks.mde.desk.MLDesktop.getInstance;
pause(0.001) %necessary to prevent some wonky behavior where Figures window wasn't undocking
desktop.setGroupDocked('Figures', dockUndockFigures);
% if desktop.isGroupDocked('Figures')~=dockUndockFigures
% disp('changing')
% end
end
end

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by