Change button color back to original value
2 次查看(过去 30 天)
显示 更早的评论
I'm trying to make a version of the game Simon in matlab and I want to make the button to return to it's original color after pushing it, however the button stays with the new color. The code I'm using is:
clc, clf, clear
h1=figure(1);
button1=uicontrol(h1, 'Style','Pushbutton', 'Units','Normalized','Position',...
[0.1 0.1 0.2 0.2],'Backgroundcolor', [1 0 0],...
'Callback',['value1=get(button1,''Value''); if value1==1',...
'set(button1,''Backgroundcolor'',[0 1 0]); else ,',...
'set(button1, ''BackgroundColor'',''r''), end,value2=get(button1,''Value'')']);
If you run it you can notice the color stays green, what can I do to return it to red automatically?
0 个评论
采纳的回答
Walter Roberson
2012-5-22
You should have a semi-colon after "if value1==1" as otherwise your code will become executed as
if value1==1set(button1,'BackgroundColor',[0 1 0]); else %and so on
2 个评论
Walter Roberson
2012-5-22
pushbuttons only activate when you release the mouse while positioned over the pushbutton. There is no callback at the time you push the button before having released it.
The only kind of uicontrol() that will callback more than once between the time of the click and the time of the release, is a slider.
If you need to detect the push of a pushbutton distinct from the release, then you need to use a figure WindowButtonDownFcn callback and WindowButtonUpFcn, and those functions will need to check to be sure that the Pointer is over the pushbutton before doing any real work. You will want to test in case the pushbutton callback is also invoked along with the WindowButtonUpFcn.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!