MATLAB Answers

How to have a GUI perform an action only when a pushbutton is pressed?

116 views (last 30 days)
sicilian27
sicilian27 on 19 Jun 2015
Commented: sicilian27 on 19 Jun 2015
Hi everyone! I am having issues coding my GUI how I would like it to be. I am trying to code a pushbutton so that it instructs a motor to run only when it is pressed down. I can send a command to the motor to start and stop, but I need Matlab to recognize when the button is pressed and when it is released. I am aware of the ButtonDownFcn property, but it does not seem to respond to a button press in the center of the ui element. Furthermore, I cannot find a ButtonUpFcn to run for this pushbutton. Does anyone have any ideas? Thanks!

Answers (3)

Walter Roberson
Walter Roberson on 19 Jun 2015
http://www.mathworks.com/help/matlab/creating_guis/write-callbacks-using-the-programmatic-workflow.html lists the available kinds of callbacks and which objects they apply to.
One possibility: you can start a timer, and each time the timer fires you can poll the Value of the pushbutton. It will go to 0 when the button is released.

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh on 19 Jun 2015
Edited: Salaheddin Hosseinzadeh on 19 Jun 2015
Hi Sicilian,
I was searching how to do the same thing a few months ago.
You want the motor to be on while you're holding the mouse click down and stop as soon as you lift up.
Roughly you have to set the enable property of the push button to inactive. Yeah I know, it sounds weird.
Then you have to add ButtonDownFcn function to the pushbutton if it's not already in the GUIDE created code. You can do this by right clicking on the pushbutton in GUIDE and find it in the view call back I believe.
The only problem is that, since the button is inactive, its appearance will not change while clicked.
apparently you also have to add a WindowButtonUpFcn to the main window (figure1) in my code, which recognize when the mouse is not down via a global variable I called buttonDown.
I attached a code and a Fig file there are 4 buttons (decrease and increase) that has the same characteristics.
Although you won't be able to run it as it should be connected to a function generator to work, however, I change the code so only the left bottom button (decrease) will work. try it ;)
You know how to do it know, it's just the matter of trying.
Good Luck.

Annie Thompson
Annie Thompson on 19 Jun 2015
Edited: Walter Roberson on 19 Jun 2015
If you use a check box instead of a pushbutton, you can used get(hObject,'Value') to return the state of the checkbox. For example:
if get(hObject,'Value') == 1
%run code here for whatever you need to ed
end
This should work.
  1 Comment
sicilian27
sicilian27 on 19 Jun 2015
Hi Annie! Yes, thank you. I was aware of the check box functionality, but I was really hoping to use a pushbutton for aesthetic and communicative purposes. Is there anyway to do this with a pushbutton? Or would it be wiser to resort to another GUI element?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!