Creating Matlab GUI: The basics

If you like this post, please visit our sponsors above. Thanks!

matlabMatlab is a very useful tool for doing a number of things and to a number of people especially researchers due to its very short development time. Having a command line driven interface however can be unlikable at times. Matlab however provides a great GUI interface and tolls to  create the GUIs.

Here is a simple 4 step method to get you started with Matlab GUI.

 

 

 

I assume some background with other GUIs and Matlab.

We will create this Matlab GUI in 4 simple steps.

image

Step 1: Creating a Blank GUI

The Matlab GUI development tool is called GUIDE. To open this tool simply type ‘guide’ on the command window.

image

Select Blank GUI from the options on the left and click OK. A blank Matlab GUI (.fig file) is created.

Note: You can also set where to save this file by checking the ‘Save new figure as:’ check box.

Step 2: Creating the Interface

Creating the GUI interface is pretty simple, we have a controls toolbar from where you simply drag and drop onto your GUI.

image

I have added three edit boxes and a push button. The goal is to create a simple GUI based adder.

You can use the static text control to create labels for you text boxes and your GUI.

image

To change the color and text on the controls, use the property inspector. The two main properties that would concern us for now are ‘String’ and ‘Tag’. ‘String’ is the display text of the control where as ‘Tag’ is the handle of the control accessible from the code.

image

Step 3: Retrieving and Validating Data

Every control has a Callback function which is called when the control is used. First we set the Callbacks for the two edit boxes and validate data.

image

   1: function edit_num1_Callback(hObject, eventdata, handles)

   2: % hObject handle to edit_num1 (see GCBO)

   3: % eventdata reserved - to be defined in a future version of MATLAB

   4: % handles structure with handles and user data (see GUIDATA)

   5: % Hints: get(hObject,'String') returns contents of edit_num1 as text

   6: % str2double(get(hObject,'String')) returns contents of edit_num1 as a double

Now we wish to retrieve the data contained in edit_num1 (the first of the two edit boxes).

   1: %get the String data from the control

   2: % and check if it is empty

   3: a = str2double(get(hObject, 'String'));

   4: if (isempty(a))

   5: %     if data is empty set the data to 0

   6:     set(handles,'String' ,'0');

   7: end

   8: guidata(hObject, handles);

We add similar code for the edit_num2.

Step 4: Adding functionality

First create the call back for the push button.

In the push button call back add the following code.

   1: % get the data of both edit boxes

   2: a = str2double(get(handles.edit_num1, 'String'));

   3: b = str2double(get(handles.edit_num2, 'String'));

   4: % add the two

   5: c = a+b;

   6: % set the data into the answer edit

   7: set(handles.edit_ans, 'String', num2str(c) );

   8: guidata(hObject, handles);

Note how we get information from the edit boxes. Also note that we have to call guidata(hObject, handles); to make the updates to the edit_ans useful.

I hope you find this tutorial useful, please feel free to leave your comments and questions.

If you like this post, please visit our sponsors blow. Thanks!

3 Responses to “Creating Matlab GUI: The basics”

  1. Ahsun says:

    nice tutorial :-)

  2. Muhammad Rizwan says:

    well boy, a good tutorial

  3. yasir arfat says:

    it help ful

Leave a Reply