How can I detect a buttonup event on a uicontrol ?

25 次查看(过去 30 天)
I would like to have an option so that I know when a uicontrol of style 'pushbutton' is pressed or released.

采纳的回答

MathWorks Support Team
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 个评论
Mukul Rao
Mukul Rao 2017-6-19
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R14SP2

Community Treasure Hunt

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

Start Hunting!

Translated by