Matlab GUI Tutorial - Disable Mouse Clicking
15 Nov 2007 Quan Quach 13 comments 3832 views
Introduction
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
-
First, download the sample GUI here. Unzip the files and place them wherever you please.
-
Now, type
guideat the command prompt.
-
Choose to open the sample GUI by clicking on “Open Existing GUI”. Click on “Browse” to locate where you saved the GUI files.

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

-
Click on the
icon on the GUI figure to bring up the accompanying .m file. -
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');
-
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);
-
Now, save your .m file and run the GUI. You should see the following GUI appear

-
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.

-
Now, type
returnat the command prompt to finish the callback. You should notice that the GUI has returned back to normal. -
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.
13 Responses to “Matlab GUI Tutorial - Disable Mouse Clicking”
Leave a Reply
Include MATLAB code in your comment by doing the following:
<pre lang="MATLAB">
%insert code here
</pre>

Excellent! How do you suggest getting around passing around the handles structure? Maybe by only passing around particular variables and updating the handles structure higher up the call tree?
Hey Mike,
I just passed the entire handles structure because in more complex GUIs, you will most likely have more than 3 buttons, and passing all those arguments could get unruly. You can also try passing each button object as parameters to the enable and disable function. Granted, the code isn’t very flexible because each time you add a new button object, the enable and disable functions have to be manually updated.
Hello,
First of all congratulations for your work, but I want to add that in my case “refresh(busyStatus)” does not work. busyStatus is not defined and also in my case, I do not want to press a key to refresh the gui.
I found drawnow where:
DRAWNOW “flushes the event queue” and forces MATLAB to update the screen.
And by taking out the lines:
disableButtons(handles);
-> refresh(busyStatus) %redraws the GUI to reflect changes
-> keyboard
enableButtons(handles);
and substituting them by drawnow it works perfect!!
disableButtons(handles);
-> drawnow();
enableButtons(handles);
Thank you!
Hi Derik,
You’re right, drawnow will work too. Thanks for the tip.
busyStatus is the name of the GUI in the example I provided, so if you named it differently, it will not work.
hello,
i tried to use your script in my gui (named gui) - but it does not work.
in my case, when i push a button, only the buttons of the first uipanel are inactive, the others als still active - and also, the inactive buttons of the uipanel dont change back in active mode - i used the keyboard and draw now function. could you please help me?
function disableButtons(handles)
%change the mouse cursor to an hourglass
set(handles.gui,’Pointer’,'watch’);
set(handles.edit_AnsCommand,’String’,'BUSY’);
%disable all the buttons so they cannot be pressed
%set(handles.pushbutton_durchflussInitialisierungStarten,’Enable’,'off’);
%set(handles.pushbutton_durchflussInitialisierungAbbruch,’Enable’,'off’);
%set(handles.pushbutton_eckpunkteUeberschreiben,’Enable’,'off’);
%set(handles.pushbutton_geometriePlotten,’Enable’,'off’);
%set(handles.pushbutton_runCommand,’Enable’,'off’);
%set(handles.pushbutton_cadAuswaehlen,’Enable’,'off’);
%set(handles.pushbutton_cadImportieren,’Enable’,'off’);
%set(handles.pushbutton_doTranslation,’Enable’,'off’);
%set(handles.pushbutton_calcRotation,’Enable’,'off’);
% ————————————————————————-
function enableButtons(handles)
%change the mouse cursor to an arrow
set(handles.gui,’Pointer’,'arrow’);
set(handles.edit_AnsCommand,’String’,'READY’);
%enable all the buttons so they can be pressed
%set(handles.pushbutton_durchflussInitialisierungStarten,’Enable’,'on’);
%set(handles.pushbutton_durchflussInitialisierungAbbruch,’Enable’,'on’);
%set(handles.pushbutton_eckpunkteUeberschreiben,’Enable’,'on’);
%set(handles.pushbutton_geometriePlotten,’Enable’,'on’);
%set(handles.pushbutton_runCommand,’Enable’,'on’);
%set(handles.pushbutton_cadAuswaehlen,’Enable’,'on’);
%set(handles.pushbutton_cadImportieren,’Enable’,'on’);
%set(handles.pushbutton_doTranslation,’Enable’,'on’);
%set(handles.pushbutton_calcRotation,’Enable’,'on’);
my pushbuttons are all using this code:
% Busy-Status
disableButtons(handles);
drawnow();
enableButtons(handles);
ex:
% — Executes on button press in pushbutton_calcRotation.
function pushbutton_calcRotation_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_calcRotation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Busy-Status
disableButtons(handles);
drawnow();
enableButtons(handles);
or
% — Executes on button press in pushbutton_doTranslation.
function pushbutton_doTranslation_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_doTranslation (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Busy-Status
disableButtons(handles);
drawnow();
enableButtons(handles);
thx for help
from what I can see, the code looks fine. please contact me throug the contact form if you need any futher assistance
coollll!!! it works. thx lots~~~!!!!!!!!!! =)
thanx alot!!!
Good job. It was helpfull for me. thx
No more rectangle switching colour from green to red. Thx a lot, fine idea and work.
Hi all,
Firstly thanks for the very helpfull forum.
Small problem, I have two GUI’s, call them A and B, and B is called directly from A. I would like to disable the buttons A until B is closed….
Any ideas…
Mac
Hey Mac,
First visit his tutorial to learn how to share data between two guis.
http://www.blinkdagger.com/matlab/matlab-gui-how-to-easily-share-data-between-two-separate-guis
I haven’t done this before, but this is how I imagine how you would do it:
When B is called from A via a pushbutton, in that callback, you would want to:
1. disable all of A’s buttons (which you learned in this tutorial)
2. call GUI B (i assume you know how to do this)
When B is closed, you want to enable A’s buttons again. you can do this by modifying the CloseRequestFcn function for GUI B.
Go here for more information on that:
http://www.blinkdagger.com/matlab/matlab-gui-tutorial-close-gui-confirmation
Hope that helps,
Quan