Change button color back to original value
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Walter Roberson
on 22 May 2012
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 Comments
Walter Roberson
on 22 May 2012
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.
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!