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

  • Posted on: 20 October 2011
  • By: Anand
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.