Change button color back to original value
조회 수: 1 (최근 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!