Borrar filtros
Borrar filtros

How can I detect a buttonup event on a uicontrol ?

25 visualizaciones (últimos 30 días)
MathWorks Support Team
MathWorks Support Team el 27 de Jun. de 2009
Comentada: Mukul Rao el 19 de Jun. de 2017
I would like to have an option so that I know when a uicontrol of style 'pushbutton' is pressed or released.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 27 de Jun. de 2009
The ability to detect a buttonup event on a uicontrol is not available in MATLAB.
To work around this issue, you can use either of the following options:
1. Use a uicontrol of style 'togglebutton' instead of a push button and perform two different actions depending on the 'Value' property of the toggle button uicontrol. The following example shows the 'Callback' function for such a toggle button uicontrol:
function togglebutton1_Callback(hObject, eventdata, handles)
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
% toggle button is pressed, perform action1
elseif button_state == get(hObject,'Min')
% toggle button is not pressed, perform action2
end
2. Alternatively, you can create a push button uicontrol with its 'Enable' property set to 'inactive'. Then, you can set the 'ButtonDownFcn' property on the uicontrol and the 'WindowButtonUpFcn' property on the parent figure. This can be done as shown in the example code below:
h = uicontrol('Style', 'pushbutton', 'String', 'My_pushbutton',...
'Position', [20 150 100 70], 'Enable', 'inactive','ButtonDownFcn','disp(''The button is pressed'')');
set(gcf,'WindowButtonUpFcn','disp(''The button is released'')');
Note that the second approach will not work when the figure is in a mode that uses the ‘WindowButtonUpFcn’ property, like Zoom, Pan, etc.
  1 comentario
Mukul Rao
Mukul Rao el 19 de Jun. de 2017
Hi Mandeguz, as of R2017a, there is no documented way to determine if a button-press or button-release event occurred for a push-button. The first workaround does require two clicks since it uses a toggle button. While the second workaround requires only one click, it will not work when the figure is in a mode that uses the ‘WindowButtonUpFcn’ as the post mentions.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R14SP2

Community Treasure Hunt

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

Start Hunting!

Translated by