Grouping Figures separately into windows and tabs
Show older comments
I have 4 plots to make, of vectors y1, y2, y3 and y4, all as a function of a vector x. I would like the first two plots to be grouped as tabs within a single Figure window, and the next two plots also grouped as tabs but in a separate window.
I tried this code:
figure
set(0,'DefaultFigureWindowStyle','docked')
plot(x,y1)
plot(x,y2)
figure
set(0,'DefaultFigureWindowStyle','normal')
plot(x,y3)
set(0,'DefaultFigureWindowStyle','docked')
plot(x,y4)
..but once tab grouping is re-enabled, plots are just added as new tabs appended to the old window rather than in a new window.
I played around with the order of the commands above but it didnt help. If anything, I only managed to get one of the plots overwritten in the same window. Please note i don't want any of the graphs to overlap, therefore "hold on" would not help.
Any suggestions? Thanks!
3 Comments
Jim Hokanson
on 15 Aug 2017
My understanding is that Matlab, by default, only has one docked figure group. A solution to this problem most likely could be achieved by using https://www.mathworks.com/matlabcentral/fileexchange/16650-setfigdockgroup That code allows creation of new docked figure group but involves some undocumented Matlab functionality.
Ajay krishna Vasanthakumar
on 30 Oct 2020
Hello can someone help how to change the tab position from top to left programtically in the docked figures
Mojgan Vaziri
on 17 Jul 2023
@Ajay krishna Vasanthakumar I also wonder that. Do you have an answer to that question by now?
Answers (4)
You can create a window, which contains several docked figures:
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
myGroup = desktop.addGroup('myGroup');
desktop.setGroupDocked('myGroup', 0);
myDim = java.awt.Dimension(4, 2); % 4 columns, 2 rows
% 1: Maximized, 2: Tiled, 3: Floating
desktop.setDocumentArrangement('myGroup', 2, myDim)
figH = gobjects(1, 8);
bakWarn = warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
for iFig = 1:8
figH(iFig) = figure('WindowStyle', 'docked', ...
'Name', sprintf('Figure %d', iFig), 'NumberTitle', 'off');
drawnow;
pause(0.02); % Magic, reduces rendering errors
set(get(handle(figH(iFig)), 'javaframe'), 'GroupName', 'myGroup');
plot(1:10, rand(1, 10));
end
warning(bakWarn);

or after hitting the icon on the top right to move all figures inside tabs (or prgrammatically: by desktop.setDocumentArrangement('myGroup', 1, myDim) ) :

This uses heavily undocumented commands. It works since at least 2009a (except for the gobjects()), but I assume I should use the EDT instead of the magic pause(0.02). I hope that Yair will improve this snippet.
16 Comments
z8080
on 28 Aug 2017
many thanks!!
Pierre-Francois
on 13 Dec 2017
Great solution !!!
Tobi
on 4 Jul 2018
Nice thing, but how can I save this complete figure? With savefig I always get an error that a valid figure handle is required.
Image Analyst
on 4 Jul 2018
Basílio Gonçalves
on 5 Dec 2018
you're the man!!!
Maciej Kaplan
on 13 Dec 2018
Edited: Maciej Kaplan
on 13 Dec 2018
Hi!
This is exactly what I am looking for, however I have a very slight problem. the figures I want to display are saved as .fig files. Hence, I have to replace the line "plot(1:10, rand(1, 10));" with something else. I have tried:
set(0,'DefaultFigureWindowStyle','docked')
for n=1:3
plot3(1:10, 1:10, rand(1,10))
saveas(gcf, ['fig' num2str(n) '.fig'])
end
for n=3:6
plot(1:10, rand(1,10))
saveas(gcf, ['fig' num2str(n) '.fig'])
end
openfig('fig1.fig','reuse'); h(1) = gcf;
openfig('fig2.fig','reuse'); h(2) = gcf;
openfig('fig3.fig','reuse'); h(3) = gcf;
openfig('fig4.fig','reuse'); h(4) = gcf;
openfig('fig5.fig','reuse'); h(5) = gcf;
openfig('fig6.fig','reuse'); h(6) = gcf;
% I also tried h(1,2,3...) = get(groot,'CurrentFigure'); insted of gcf
close all;
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
myGroup = desktop.addGroup('myGroup');
desktop.setGroupDocked('myGroup', 0);
myDim = java.awt.Dimension(3, 2); % 3 columns, 2 rows
% 1: Maximized, 2: Tiled, 3: Floating
desktop.setDocumentArrangement('myGroup', 2, myDim)
figH = gobjects(1, 6);
bakWarn = warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
for iFig = 1:6
figH(iFig) = figure('WindowStyle', 'docked', ...
'Name', sprintf('Figure %d', iFig), 'NumberTitle', 'off');
drawnow;
pause(0.02); % Magic, reduces rendering errors
set(get(handle(figH(iFig)), 'javaframe'), 'GroupName', 'myGroup');
h(iFig)
end
warning(bakWarn);
I do get the tiled 3 by 2 window that I want, however the tiles are all empty.
I am thankful for any help!
EDIT: creating figures for the code to be executable.
Image Analyst
on 13 Dec 2018
Maciej, you forgot to attach your figures. Make it easy for Jan to help you, not hard. Don't force him to have to create figures for you when you already have them.
Maciej Kaplan
on 13 Dec 2018
Edited: Maciej Kaplan
on 13 Dec 2018
Image Analyst, thank you! I'm sorry! I have now modified my first post. The code now creates the figures! What I basically wish for is 6 figures in one window, and then I want a second window with 6 more figures.
I included the plot3 since 3 of my plots are 3D-plots. I couldnt understand how to get the subplot to solve my problem (maybe it is possible..?).
Veronica Taurino
on 2 Mar 2021
@Jan I am using your solution. However, since there are several "undocumented commands" as you said, could you please help me to understand how to change position to the docked group of figures? I read for docked figure you can't define position, so I created several figures in desired position. But when I dock them, I lose position and size. Could you help me?
Giuseppe Naselli
on 16 Jun 2021
Edited: Giuseppe Naselli
on 16 Jun 2021
Hi @Jan
do you know a method to save all tiled figures in 1 single png or jpeg or any other format?
(from a function or script)
I tried with
img = getframe(gcf);
imwrite(img.cdata, [pwd '\pippo.png']);
But obviously gcf gets the last figure I tiled in my window
Thanks
G
Jan
on 16 Jun 2021
@Giuseppe Naselli: I did not get the above code to run reliably in R2018b. But if you are able to do this, don't use gcf for accessing the single figures, but figH(iFig) .
Image Analyst
on 17 Jun 2021
@Giuseppe Naselli, try exportgraphics().
Roman Voronov
on 24 May 2022
Add a
while isempty(figH(iFig).Children)
%waiting for figure to be plotted
end
in there. Or, if your figures take a long time to generate, it will put them into the wrong tabs.
Sylvain
on 29 May 2022
This is an astonishing example you provided.
I have tried to replace the figures buy uifigures, however, this is not supported by MATLAB.
Still looking for a workaround to tile both uifigures and figures (reason is to use the GUI Layout toolbox with the flex panels and uitrees)
Kristoffer Florvaag-Dybvik
on 31 Jan 2023
Thank you!
Azzi Abdelmalek
on 4 Oct 2014
Use hold on
figure
set(0,'DefaultFigureWindowStyle','docked')
plot(x,y1)
hold on
plot(x,y2)
2 Comments
AwedBy Matlab
on 4 Oct 2014
Jrod H
on 1 Apr 2022
Well, the set( ) function did exactly what I wanted!! Works great. Do you know what command to pass to get the windows to display in "Tile" mode rather than as tabs?
Matthew
on 19 Jun 2017
1 vote
I'm not too familiar, but I think using the "docked" approach to getting these tabbed figures, you will not be able to accomplish what you are trying to do. Any "docked" figures will all end up in one top level figures window. Any "normal" figures can exist on their own. You can not have two figure windows each with docked figures.
I think you could accomplish what you are trying to do with uifigures, but they may lack other capabilites you are looking for. See "Graphics Support in App Designer" in the matlab help for discussion of what uifigures do and do not support.
Image Analyst
on 4 Oct 2014
0 votes
MATLAB does not have a tab control. The best you can do is a panel. Put your stuff into panels and then set the visibility of each panel on or off using set(hPanel, 'Visibility', 'off') that is in the callback of something like checkboxes or pushbuttons that you put into an array atop where the panels go.
Alternatively you could use a new figure for each set of plots (I'd probably not have more than about an array of 3 by 4 on each window) and use subplot() to plot into the desired plot on that figure. Let me know if you don't know how to do this and it's what you want.
1 Comment
AwedBy Matlab
on 4 Oct 2014
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!