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

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.

 Risposta accettata

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 Commenti

Ah I'd found this, too, but unfortunately Matlab considers COM to be legacy (see http://www.mathworks.com/javaframe), and so may be depracated. I've since been searching for a longer-term solution. Sorry I should've put this in the original question, but I'll leave as-is to give you credit.
The COM referred to there is actxcontrol which is not connected to com.mathworks facilities.
@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!
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.

Accedi per commentare.

Più risposte (2)

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 Commenti

set(findall(groot, 'type', 'figure', '-and', 'WindowStyle', 'docked'), 'WindowStyle', 'normal')
No loop needed.
Again, I'm not asking how to dock/undock individual figure windows. I'm asking how to dock/undock the Figures window that holds other figure windows.
I don't think the Figures window can be docked as such. I believe what happens is that each window is docked independently, with the Figures window disappearing if there are no remaining figures in it.
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.
I am not familiar with a programmatic way to dock the entire figure window.

Accedi per commentare.

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

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by