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 件のコメント
Paulo Silva
2011 年 2 月 17 日
That's a good question, I tried to find a way but failed :(
回答 (1 件)
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 件のコメント
Matt Tearle
2011 年 2 月 17 日
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.
Paulo Silva
2011 年 2 月 17 日
I'm saving that nice code, might be useful someday :)
Kenneth Eaton
2011 年 2 月 17 日
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.
Jiro Doke
2011 年 2 月 17 日
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!