Can I create a momentary command using the GUI pushbutton?

Hello all,
Newbie GUI developer here. I am attempting to create a GUI with a pushbutton which emulates the action of a spring return button. I prefer to use the pushbutton over the toggle button as it more closely emulates the on screen behavior that I want. The GUI would have the following characteristics:
1. While the pushbutton is pressed down the value of a constant block in the simulink model is set to 1. This I can easily do by utilizing a set_param('MyModel/ConstantBlock','Value','1') construct.
2. When the mouse button is released the value of the constant block is returned to 0.
This is where I am having difficulty as there does not appear to be "ButtonUp" functionality associated with a pushbutton. Any thoughts?
Thanks in advance,
Don Simon

댓글 수: 1

That's a good question, I tried to find a way but failed :(

댓글을 달려면 로그인하십시오.

답변 (1개)

Jiro Doke
Jiro Doke 2011년 2월 17일
One way is to make use of WindowButtonUpFcn property of the figure along with ButtonDownFcn of an inactive uicontrol (toggle button).
function testtest
f = figure;
h = uicontrol('style', 'togglebutton', ...
'enable', 'inactive', ...
'buttondownfcn', @buttondown);
function buttondown(obj, edata)
set(h, 'value', 1); % press the toggle button
set(f, 'WindowButtonUpFcn', @buttonup)
disp('button is pressed');
end
function buttonup(obj, edata)
set(h, 'value', 0) % release the toggle button
set(f, 'WindowButtonUpFcn', '');
disp('button is released');
end
end

댓글 수: 4

Jiro, you're too fast for me. I posted almost the same answer, only to discover that it was already there!
Anyway, one simplification is that you can set both the windowbuttonupfcn and windowbuttondownfcn properties at the beginning, and just leave them.
I'm saving that nice code, might be useful someday :)
There can often be a number of other features in MATLAB that make use of the WindowButton(Down/Up)Fcn callbacks (such as plot rotation). To avoid breaking these other features that may be active at the same time, it would be better to store the old value of WindowButtonUpFcn before you change it, then restore it to this previous value after you are done instead of setting it to empty.
Good point. Yes, that was partially the reason why I have it remove WindowButtonUpFcn upon button up. I also didn't want it to keep firing if I was clicking elsewhere on the GUI. But to be complete, I should do what Kenneth suggested, which is to reset it back to what it was.
Now, I want to do this right. I can edit and change my answer to reflect that. But if I do that, your comment will sound weird.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

질문:

Don
2011년 2월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by