Building a Simple GUI Interface in MATLAB (guide)

Yes, building a simple GUI interface through MATLAB is possible. Just like in Visual Basic or Visual C++, you have a drag & drop feature of buttons, text boxes etc, & then you just need to program them in order to define their functionality. If you want to export it as a standalone executable file, you can even do that. But just like the other languages the application build with MATLAB will require its runtime, to be pre-installed.

So, the GUI interface builder for the MATLAB is known by the name "GUIDE". The fullform of GUIDE is "Graphical user interface design environment".

As the MATLAB official help document states about it:
guide is both a directory and a function.
    Calling guide by itself will open the guide Quick Start Dialog where you can choose to open a previously created GUI or create a new one from templates.
guide(filename) opens the FIG-file for editing where you can build up the GUI & related function.

* Just type in the command window with the command & you will see a pop-up appearing like this.
Guide Quick Start Box in MATLAB
Guide Quick Start Box in MATLAB
* Select the "Blank GUI" option, which is also being selected by default.
* Then press OK & you will see a GUI workspace being opened up in front of you.

GUI building workspace in MATLAB: guide
GUI building workspace in MATLAB: guide


      * In left hand side there are many types of button interfaces are there. As according to your requirement pick any button and drop in grid area.

NOTE: There are two interfaces in GUI. One is figure and other one is MATLAB code.


Lets take an example, We just want to build a simple GUI that prints your name by a click of button which is present in the GUI.

STEP 1: Take two push buttons & drag them up in the grid box as shown in the above figure.
STEP 2: Tale one static text box & drag it, over the grid box.
STEP 3: Left click on any button (that you have just got in the grids) and go to property inspector. By the help of this you can change various properties of it like name, color, font size etc.
STEP 4: Clear the section of "static text" box by clearing string in property inspector.



Changing the properties of various objects in MATLAB GUI through Property Inspector
Changing the properties of various objects in MATLAB GUI through Property Inspector
STEP 5: Optional (You can change the size of the button, by dragging the corner points of the button bounding box)
STEP 6: Drag another button (Optional, depends upon your requirement)
Now the resultant GUI will Look like this:
GUI Development in MATLAB
GUI Development in MATLAB
STEP 7: Now the crucial step, go to the "editor" (Right Click on the grid boxes & Select editor) to change in the call back function of the button, which will tell them what to do. It will ask you to save the file with the extension ".fig". Save it & then it will automatically opens the MATLAB editor window, there you have to make changes in the code in order to get the things work.

GUIDE editor option
GUIDE editor option
Here is, what we did in our case (The changes done & added are in BOLD):


function varargout = test1(varargin)
% TEST1 MATLAB code for test1.fig
%      TEST1, by itself, creates a new TEST1 or raises the existing
%      singleton*.
%
%      H = TEST1 returns the handle to a new TEST1 or the handle to
%      the existing singleton*.
%
%      TEST1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TEST1.M with the given input arguments.
%
%      TEST1('Property','Value',...) creates a new TEST1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before test1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to test1_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help test1

% Last Modified by GUIDE v2.5 05-Apr-2015 01:53:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test1_OpeningFcn, ...
                   'gui_OutputFcn',  @test1_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before test1 is made visible.
function test1_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 test1 (see VARARGIN)

% Choose default command line output for test1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test1_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.text1,'string','vijay sharma');

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.text1,'string','vibhutesh');
 

Step 8: Now Run it, you can see a play button in the MATLAB's GUI building Interface, just click once & it will execute your GUI.
GUI Designed in MATLAB with 2 Buttons & a Static Text Box to Display the Output
GUI Designed in MATLAB with 2 Buttons & a Static Text Box to Display the Output




Press push button. And see the output!

NOTE: In MATLAB GUI all variables to be displayed should be in string Data Type. So if you taking and number so change it into string.

0 comments: