How to have a GUI perform an action only when a pushbutton is pressed?
31 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(3 个)
Salaheddin Hosseinzadeh
2015-6-19
编辑:Salaheddin Hosseinzadeh
2015-6-19
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.
0 个评论
Walter Roberson
2015-6-19
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.
0 个评论
Annie Thompson
2015-6-19
编辑:Walter Roberson
2015-6-19
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!