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?

采纳的回答

Walter Roberson
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 个评论
Adrian
Adrian 2012-5-22
I just tried it and continues to hold the green color after I push it. What I want to do is for the button to change the color only when I push it but to go back as soon as I let it go.
Walter Roberson
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 CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by