Writing the code for the GUI

MATLAB automatically generates an .m file to go along with the figure that you just put together. The .m file is where we attach the appropriate code to the callback of each component. For the purposes of this tutorial, we are primarily concerned only with the callback functions. You don’t have to worry about any of the other function types.

  1. Open up the .m file that was automatically generated when you saved your GUI. In the MATLAB editor, click on the Function Icon icon, which will bring up a list of the functions within the .m file. Select input1_editText_Callback.

    Function Shortcut

  2. The cursor should take you to the following code block:

    function input1_editText_Callback(hObject, eventdata, handles)
    % hObject    handle to input1_editText (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
     
    % Hints: get(hObject,'String') returns contents of input1_editText as text
    %        str2double(get(hObject,'String')) returns contents of input1_editText as a double

    Add the following code to the bottom of that code block:

    %store the contents of input1_editText as a string. if the string 
    %is not a number then input will be empty
    input = str2num(get(hObject,'String'));
     
    %checks to see if input is empty. if so, default input1_editText to zero
    if (isempty(input))
         set(hObject,'String','0')
    end
    guidata(hObject, handles);

    This piece of code simply makes sure that the input is well defined. We don’t want the user to put in inputs that aren’t numbers! The last line of code tells the gui to update the handles structure after the callback is complete. The handles stores all the relevant data related to the GUI. This topic will be discussed in depth in a different tutorial. For now, you should take it at face value that it’s a good idea to end each callback function with guidata(hObject, handles); so that the handles are always updated after each callback. This can save you from potential headaches later on.

  3. Add the same block of code to input2_editText_Callback.

  4. Now we need to edit the add_pushbutton_Callback. Click on the Function Icon icon and select add_pushbutton_Callback. The following code block is what you should see in the .m file.

    % --- Executes on button press in add_pushbutton.
    function add_pushbutton_Callback(hObject, eventdata, handles)
    % hObject    handle to add_pushbutton (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)

    Here is the code that we will add to this callback:

    a = get(handles.input1_editText,'String');
    b = get(handles.input2_editText,'String');
    % a and b are variables of Strings type, and need to be converted 
    % to variables of Number type before they can be added together
     
    total = str2num(a) + str2num(b);
    c = num2str(total);
    % need to convert the answer back into String type to display it
    set(handles.answer_staticText,'String',c);
    guidata(hObject, handles);
  5. Let’s discuss how the code we just added works:

    a = get(handles.input1_editText,'String');
    b = get(handles.input2_editText,'String');

    The two lines of code above take the strings within the Edit Text components, and stores them into the variables a and b. Since they are variables of String type, and not Number type, we cannot simply add them together. Thus, we must convert a and b to Number type before MATLAB can add them together.

  6. We can convert variables of String type to Number type using the MATLAB command str2num(String argument). Similarly, we can do the opposite using num2str(Number argument). The following line of code is used to add the two inputs together.

    total= (str2num(a) + str2num(b));

    The next line of code converts the sum variable to String type and stores it into the variable c.

    c = num2str(total);

    The reason we convert the final answer back into String type is because the Static Text component does not display variables of Number type. If you did not convert it back into a String type, the GUI would run into an error when it tries to display the answer.

  7. Now we just need to send the sum of the two inputs to the answer box that we created. This is done using the following line of code. This line of code populates the Static Text component with the variable c.

    set(handles.answer_staticText,'String',c);

    The last line of code updates the handles structures as was previously mentioned.

    guidata(hObject, handles);

    Congratulations, we’re finished coding the GUI. Don’t forget to save your m-file. It is now time to launch the GUI!

  8. If you don’t want MATLAB to automatically generate all those comments for each of the callbacks, there is a way to disable this feature. From the GUI editor, go to File, then to Preferences.

    Disable Automatic Comments

Pages: 1 2 3 4 5