Wednesday, 19 October 2011

Multipages GUI forms: combining from muliple GUI m files : Links approach

We have learned to create a multipage form single GUI. This is not a very clean approach, because all the panels are on same position and it is sometime very hard to edit one panel later on. This example shows you to create different GUI for different  pages/slides and create a master gui to control them.

(See the previous Note on Multipage GUIs, at

  Each GUI has a structure called handles. To see or edit this  structure , you can call guidata.
gui_reference is a double number which works as a pointer to the gui. To store this pointer as a variable, you need to call gui with an output
Note that gui's handles.output also contain this pointer. You can use this output when you are writing code inside that gui.

Creating Slide Pages
 First of All create two guis both (GUI1.m and GUI2.m) with two edit box edit1,edit2 and two pushbutton pushbutton1 with title Next and second  pushbutton pushbutton2 with title Previous.

Now for navigation we will add too fields to each gui, next and prev.
So first in OpeningFcn add this two lines both in gui1 and gui2
function gui1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to a1 (see VARARGIN)

% Choose default command line output for a1
handles.output = hObject; = 1 ;
handles.prev = 1 ;

% Update handles structure
guidata(hObject, handles);

Now go to pushbutton1_callback and add these line for displaying the next slide and disappearing itself
for pushbutton2_callback do this to display previous slide.
Creating Master Slide
Now create a blank GUI with a pushbutton titled "Start"  and save it as gui0.fig. This will automatically create a gui0.m file.

Now in this file, go to guiOpeningFcn
and you will find the followin code there
function gui0_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to gui0 (see VARARGIN)
% Choose default command line output for gui0
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

Now edit this code to call two slides and save their references to it.
% Choose default command line output for a1
handles.output = hObject;

handles.s1= gui1;
handles.s2= gui2;

h1=guidata(handles.s1); = handles.s2;
h1.prev = hObject;

h2.prev = handles.s1; = hObject;

% Update handles structure
guidata(hObject, handles);
set(handles.s2,'Visible','off');guidata(hObject, handles);
As you can see, I have called gui1 and gui2 and saved there reference or pointers to handles.s1 and handles.s2. Now for navigation, we added the s2 to the next of s1(slide 1) and mastergui as previous of s1. Similarly for s2.

Now we need to code the start button to hide the master page and start the silde1.
Here it is .. simple enough now..

remember that handles.output indicate the pointer to gui itself.

Final Touch: Processing your data
Now once you are done , you return to master page again because next slide of the s2 is masterpage.
Here you need to collect all the data and do whatever you want. As an example I will add all the 4 number together which were written in 4 editboxes. For easeness, Put one more button titled "Submit" and one Editbox titled Result on masterpage and write the calculation code in Submit_callback. Needless to say that you need to press this submit button once you are back on masterpage to do the final editing.

Here is the calculation code for your reference
Here you are done. The benefit of the master page is that you can rearrange your slides very easily and each gui is independent.


  1. I have built some gui program
    first is of login & second is of simple gui. now i want to link both in such a manner that first login & if login is successful then go to second gui using same program.
    but i cann't do this.
    if you can then please help me.
    i'm waiting for your response.

    links for programs are


  2. @rajiv

    this is pretty easy. Write a check code in the login pushbutton which varifies the
    login details. if the password matches, call the other gui by writing just the gui name
    suppose ur second gui is named gui2

    write this in ButtonLogin_callback()

    function ButtonLogin_callback()
    %write other stuff to verify
    Login_success=1; % change this to actually verify the login
    if (Login_success)

  3. hi buddy.
    please help me.

    % --- Executes on button press in login.
    function login_Callback(hObject, eventdata, handles)
    % hObject handle to login (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)
    ID = get(handles.edit1,'string');
    PW = get(handles.edit2,'string');
    if strcmp(ID,'admin') && strcmp(PW,'admin')==1
    msgbox('login sukses');
    errordlg('Invalid username or password');

    gui2 (aplikasi.fig) does not show up when a successful login.

  4. I want to enter username in giu and that wiil be save after in database with want change in next slie..How to do that?

  5. thanks alot code is really helpful:):)

  6. yeah..its really helpful..but the problem I got, for multiple GUIs it shows all the windows order wise (visible),,then it works as visibility defined. But primarily it opens all the windows. It will be very helpful if u kindly help me..

  7. Hello,
    thanks, this code is indeed very helpful.
    My problem is, that each time I run the Start-GUI, it opens/runs all the other pages, too.
    But the other pages need input from the previous pages, so it doesn't work.
    How can I change that?
    I'd be very grateful for every idea!!

  8. Hello guys hope anyone can help me I'm beginner !!where can I put this code in my GUI