Change button color back to original value

조회 수: 1 (최근 30일)
Adrian
Adrian 2012년 5월 22일
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