How can I detect a buttonup event on a uicontrol ?

38 views (last 30 days)
I would like to have an option so that I know when a uicontrol of style 'pushbutton' is pressed or released.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 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 Comment
Mukul Rao
Mukul Rao on 19 Jun 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R14SP2

Community Treasure Hunt

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

Start Hunting!