Borrar filtros
Borrar filtros

How to create a switch until released button in Matlab GUI

1 visualización (últimos 30 días)
himesh
himesh el 29 de Oct. de 2014
Respondida: matt dash el 29 de Oct. de 2014
Hi, I am trying to create a button in the GUI where random numbers appear in the output as the user holds down a button.
I have used a toggle button with the code:
while get(hObject,'Value')
disp(rand)
pause(0.001)
end
This code works, but the output runs as the user presses the button and carries on until the button is pressed again.
How do i create a button where the code only loops while the button is being pressed down? As soon as the mouse click is released, the code stops executing

Respuestas (2)

Sean de Wolski
Sean de Wolski el 29 de Oct. de 2014
There's not really a straight-forward way to do that. It can be done with a patch made to look like a uicontrol whose buttondownfcn changes the windowbuttonupfcn like in this example:
However, it would be better if it could just have a toggle button that when toggled stops:

matt dash
matt dash el 29 de Oct. de 2014
As sean said, this is not an easy thing to do. The problem is that since your code is continually running in a loop, it is keeping matlab busy, and it can only be interrupted by an event on the java event dispatch thread. The reason your code with the toggle button works is that toggling a button is such an event.
But to get it to detect the mouse button being released is more complicated. I think you would need to set up a MouseListener in java. I dont think the windowbuttonupfcn method will work here since Matlab will be stuck in your while loop, it will never process the windowbuttonupfcn.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by