Creating Tabs using GUIDE

Matlab GUIDE Tab 1

Combining GUIDE GUIs into Tabs

Last year a client asked me to add a tab to an existing GUI meant for robot control. This wouldn’t be a big deal, but the existing GUI was made using GUIDE and I didn’t want to programme the layout from scratch. Unfortunately GUIDE doesn’t support tab creation (at least as of 2015b); but I found a small hack that can help fuse two GUIs created using GUIDE as two tabs in a single window.

The idea is to export the GUIs into m-files using Export option in File menu, create tabs using uitab and replace the parent of objects from each GUI to the respective tabs. For demonstrating the trick here, I chose two samples GUIDE offers – GUI with Uicontrols & GUI with Axes & Menu.

Creating Tabs using Matlab GUIDE - UIControls

Gui 1

Creating Tabs using MATLAB GUIDE - Axes & Menu

Gui 2

Choose each one, save them (say as Gui1 & Gui2). Export each of them using File->Export, as say Gui1_export.m & Gui2_export.m.  Let’s first open Gui1_export.m. The exported file contains all the content of Gui1.m and a few more functions that define layout of the GUI. The first of these is  LayoutFcn that appears in Gui1_export.m immediately after all functions of Gui1.m. 

% --- Creates and returns a handle to the GUI figure.
function h1 = Gui1_export_LayoutFcn(policy)
% policy - create a new figure or use a singleton. 'new' or 'reuse'.
persistent hsingleton;

Here, you will find handles of objects being created starting with h1 = figure(… , h2 = axes(…, h3  etc. h1 will be handle to the figure window and is parent for rest of the objects (you can notice that ‘Parent’ field for each subsequent object handle is set to h1). After defining h1 we create tabs using uitabgroup and uitab as follows.

%%% %%% %%% %%% %%% %%% TAB CREATION %%% %%% %%% %%% %%% %%%
ht = uitabgroup('parent',h1,'Position',[0 0 1 .95]);
h1tab = uitab(ht,'title','GUI 1');
h2tab = uitab(ht,'title','GUI 2');

For the object handles h2, h3, h4 etc whose definitions follow, we will replace h1 with h1tab as parent. E.g.

h2 = uicontrol(...
'Parent',h1tab,...

(You may do find and replace, but be careful and don’t replace h1 with h1tab in hsingleton = h1 statement)

Running the Gui1_export.m at this point shows two tabs with titles GUI 1 & GUI 2, with GUI 1 selected as the default tab. Selecting GUI2 will show an empty figure as GUI2 objects have not been added yet. Now, go to LayoutFcn of Gui2_export.m and replace parents of object handles h2, h3, etc. with h2tab. Copy the text from “appdata = [ ];” immediately preceeding definition of h2 in LayoutFcn to Gui2_export.m until the end of function except for hsingleton = h1; line.

Combining GUIDE GUIs into Tabs

Open Gui1_export.m and right after definitions of Gui1 objects and before hsingleton definition, load Gui2_export.mat* and paste the text copied from Gui2_export.m as shown below.

* mat files are generated during export, but in this particular example it isn’t generated and hence this  can be skipped.

How to Fuse GUIDE GUIs into tabs

Now run the file Gui1_export.m and the GUI figure with two tabs shows up. I had to edit the positions of objects on GUI 2 tab. Many variable names used by handles of Gui1 will be overwritten by Gui2 and will hence be lost, but this never created me any problems. Note that all callbacks of Gui2 objects are configured to refer Gui2_export.m and hence this file will be required for functioning of callbacks of Gui2 objects.

Matlab GUIDE Tab 2

I attached the scripts and mat files that run fine in R2012b (in later versions they might produce warnings and might produce errors in earlier versions). Gui1_export.m Gui2_export.m Gui1_export.mat

Learn Matlab Tips & Tricks

2 thoughts on “Creating Tabs using GUIDE

  1. E.B

    Hi,
    thank you very much for this detailed solution. it is very useful. I am doing what you have suggested to combine 3 GUI into tabs. It works fine, the first tab adapts the dimension into the new tab window however the two other tabs show truncated gui. do you have any solutions for that ?

    thank you!

    Reply
  2. cK Post author

    The parent figure window must be slightly larger than the largest of all guis. Moreover, to avoid issues like this I disable resizing on GUIs so that all elements come up in their intended positions and sizes.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>