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 http://www.facebook.com/notes/matlab-by-examples-book/gui-working-on-multislides-form/155932614460266)

Handles
 Remember:
  Each GUI has a structure called handles. To see or edit this  structure , you can call guidata.
GET HANDLES
h=guidata(gui_reference);
SET HANDLES
guidata(gui_reference,h);
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
gui_reference=gui1;
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;
handles.next = 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
set(handles.next,'Visible','On');
set(handles.output,'Visible','Off');
for pushbutton2_callback do this to display previous slide.
set(handles.prev,'Visible','On');
set(handles.output,'Visible','Off');
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);
h1.next = handles.s2;
h1.prev = hObject;
guidata(handles.s1,h1);


h2=guidata(handles.s2);
h2.prev = handles.s1;
h2.next = hObject;
guidata(handles.s2,h2);

% Update handles structure
guidata(hObject, handles);
handles.output
%set(handles.output,'Visible','off');
set(handles.s1,'Visible','off');
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..

set(handles.output,'Visible','off');
set(handles.s1,'Visible','on');
set(handles.s2,'Visible','off');
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
h1=guidata(handles.s1);
a1=get(h1.'edit1','String');
a2=get(h1.'edit2','String');
h2=guidata(handles.s2);
a3=get(h2.'edit1','String');
a4=get(h2.'edit2','String');
a=str2num(a1)+str2num(a2)+str2num(a3)+str2num(a4);
set(handles.result,'String',a);
Here you are done. The benefit of the master page is that you can rearrange your slides very easily and each gui is independent.

16 comments:

  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
    (i) http://rajivcan.blogspot.in/2012/04/login-program-using-matlab-gui.html

    (ii) http://rajivcan.blogspot.in/2012/04/simple-gui-program-using-matlab.html

    ReplyDelete
  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)
    gui2;
    set(handles.figure,'Visibility',false);
    end

    ReplyDelete
  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');
    aplikasi.fig;
    set(handles.figure,'Visibility',false);
    else
    errordlg('Invalid username or password');
    end


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

    ReplyDelete
  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?

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

    ReplyDelete
  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..

    ReplyDelete
  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!!

    ReplyDelete
  8. Hello guys hope anyone can help me I'm beginner !!where can I put this code in my GUI
    h=guidata(gui_reference);
    guidata(gui_reference,h);
    gui_reference=gui1;

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Hotel front desk software

    Front Desk is a comprehensive practice management solution offering complete patient satisfaction, with the tools you need to connect with staff and provide the exceptional customer service your patients deserve. ... Read more about the Front Desk Practice Management System®.

    ReplyDelete
  11. Wow, so beautiful and wonderful post! Thanks for giving an opportunity to read a fantastic and imaginary blog. It gives me lots of pleasure and interest. Thanks for sharing. If you need any technical support related QuickBooks, click here, QuickBooks Customer Service Number for immediate solution.

    ReplyDelete
  12. Hey! Fabulous post. It is the best thing that I have read on the internet today. Moreover, if you need instant support for QuickBooks Error, visit at QuickBooks Customer Service Our team is always ready to help and support their clients.

    ReplyDelete
  13. Thanks for sharing such useful information with us. I hope you will share some more info about your blog. Please keep sharing. We will also provide QuickBooks Support Phone Number for instant help.

    ReplyDelete
  14. Hey! Excellent work. Being a QuickBooks user, if you are struggling with any issue, then dial QuickBooks Customer Support Number Our team at QuickBooks will provide you with the best technical solutions for QuickBooks problems.

    ReplyDelete
  15. Hey! Nice Blog, I have been using QuickBooks for a long time. One day, I encountered QuickBooks Enterprise Support in my software, then I called QuickBooks Support Phone Number (855)538-8273. They resolved my error in the least possible time.

    ReplyDelete