Introduction

Matlab Logo This tutorial will show you how to implement a “busy” status wherein the user is informed that the Matlab GUI is processing a task, and cannot accept more tasks until the current task is complete. This will be accomplished by disabling all of the buttons on the GUI and changing the mouse pointer to an hourglass icon. This feature is useful when you have a function callback that takes a couple of seconds to run. During this time, you want to prevent the user from pressing other buttons that may break your GUI or interrupt your function.

This tutorial is written for those with some experience creating a Matlab GUI. If you’re new to creating GUIs in Matlab, you should visit this tutorial first. Basic knowledge of Matlab and an understanding on how data is shared among callbacks is highly recommended. Matlab version 2007a is used in writing this tutorial. Both earlier versions and new versions should be compatible as well (as long as it isan’t too outdated). Let’s get started!

Disabling and Enabling Buttons, Changing the Mouse Cursor

  1. First, download the sample GUI here. Unzip the files and place them wherever you please.

  2. Now, type guide at the command prompt.

    Command prompt

  3. Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

    GUIDE Screen

  4. Here is what the GUI should look like when you open it:

    GUI Figure

  5. Click on the mfile icon icon on the GUI figure to bring up the accompanying .m file.

  6. Now, add the following code to the end of the .m file. I wrote here that I was strongly against writing functions in this manner, but I think this is a reasonable exception.

    function disableButtons(handles)
    %change the mouse cursor to an hourglass
    set(handles.figure1,'Pointer','watch');
     
    %disable all the buttons so they cannot be pressed
    set(handles.pushbutton1,'Enable','off');
    set(handles.pushbutton2,'Enable','off');
    set(handles.pushbutton3,'Enable','off');
     
    function enableButtons(handles)
    %change the mouse cursor to an arrow
    set(handles.figure1,'Pointer','arrow');
     
    %enable all the buttons so they can be pressed
    set(handles.pushbutton1,'Enable','on');
    set(handles.pushbutton2,'Enable','on');
    set(handles.pushbutton3,'Enable','on');
  7. Now, add the following code to each of the button callback functions.

    disableButtons(handles);
    refresh(busyStatus) %redraws the GUI to reflect changes
    keyboard
    enableButtons(handles);
  8. Now, save your .m file and run the GUI. You should see the following GUI appear

    GUI

  9. Press any of the buttons. You should see the following change reflected on your GUI now. Notice that the mouse cursor has also changed to an hourglass icon.

    GUI Buttons Disabled

  10. Now, type return at the command prompt to finish the callback. You should notice that the GUI has returned back to normal.

  11. If you plan on using this code in your own GUI, you won’t be needing the “keyboard” command. Instead, your code would look this:

    disableButtons(handles);
    refresh(busyStatus) %redraws the GUI to reflect changes
     
    %insert your code here, "keyboard" is not needed
    %it was merely used as a demonstration example
     
    %during this part of your code, the buttons are disabled
    %when your code is done running, we enable the buttons again
     
    enableButtons(handles);

This is the end of the tutorial.