How to have a GUI perform an action only when a pushbutton is pressed?
41 views (last 30 days)
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!
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 on 19 Jun 2015
Edited: Salaheddin Hosseinzadeh on 19 Jun 2015
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.