MATLAB GUI (Graphical User Interface)
Tutorial for Beginners
By Prof. S. K. Dwivedy
IIT Guwahati
Preprocessing Data:
Why use a GUI in MATLAB?
It makes things simple for the end-users of the program.
Suitable for solving optimization problems.
Initializing GUIDE(GUI Creator)
1. Open up MATLAB. Go to the command window and type in guide.
2. Choose the first option Blank GUI (Default)
Initializing GUIDE (GUI Creator)
3. You should now see either of the following screen:
Initializing GUIDE (GUI
Creator)
4. Before adding components blindly, it is good to have a rough idea
about how you want the graphical part of the GUI to look like:-
Creating the Visual Aspect of the GUI: Part 1
1. For the adder GUI, we will need the following components
Four Static text component
Two Edit text component
One Pushbutton component
Creating the Visual Aspect of the GUI: Part 1
2. Edit the properties of these components.
Double click one of the Static Text components. You should see the
property Inspector.
Creating the Visual Aspect of the GUI: Part 1
3. Do the same for the next Static Text component, but instead of changing
the String parameter to +, change it to =, and another it to MyAdderGUI.
4. For Static Text component 0, modify the Tag parameter to
answer_staticText.
5. You will have a panel that
look like the following-
Creating the Visual Aspect of the GUI: Part 2
1. Modify the Edit Text components.
Double click on the first Edit Text component, Set the String parameter to 0
Change the Tag parameter to input1_editText
2. The second Edit Text component, set the String parameter to 0
Set the Tag parameter input2_editText.
Creating the Visual Aspect of the GUI: Part 2
3. Modify the pushbutton component.
Change the String parameter to Add!
Change the Tag parameter to add_pushbutton.
Creating the Visual Aspect of the GUI: Part 2
4. You should have something 5. Rearrange the components as follows:
like this:
5. Save your GUI under any file name. I choose to name mine as
myAdder. When you save this file, MATLAB automatically generates two
files:
myAdder.fig and myAdder.m
The .fig file contains the graphics of your interface.
The .m file contains all the code for the GUI.
Writing the Code for the GUI Callbacks
1. Open up the .m file that was automatically generated when you saved
your GUI.
2. In the MATLAB editor, click on the icon, which will bring up a
list of the functions within the .m file. Select
input1_editText_Callback
Writing the Code for the GUI Callbacks
3. The cursor should take you to the following code block:
4. Add the following code to the bottom of that code block:
5. Add the same block of code to input2_editText_Callback.
Writing the Code for the GUI Callbacks
6. Now we need to edit the add_pushbutton_Callback.
Here is the code that we will add to this callback
Launching the GUI
7. There are two ways to launch your GUI.
The first way: Press the icon on the GUIDE editor
The second method : Launch the GUI from the MATLAB command
prompt.
Type in the name of the GUI at the command prompt.
Launching the GUI
8. The GUI should start running immediately:
Some of the GUI shown below:
GUI Panels
GUI Panels
Thank you