Update Background colour Button
조회 수: 11 (최근 30일)
이전 댓글 표시
Hello,
I would like to change the background colour of a button depending of the value of a variable. This variable changes every 3 s from the script:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global flag
global estado
button; %% run gui %%
flag=0;
estado=0;
while flag==0
check(estado) %%displays the value of the variable %%
estado=not(estado);
pause(3)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
This is my callback code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in boton.
function boton_Callback(hObject, eventdata, handles)
% hObject handle to boton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global estado;
boton_estado=estado;
if (boton_estado == 1)
set(handles.boton,'BackgroundColor','r');
else
set(handles.boton,'BackgroundColor','g');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
but it only works when I press the button :S would it be possible that the colour change at the same time that 'estado' without pressing any key??
Thanks a lot guys!
댓글 수: 0
답변 (2개)
Jan
2013년 7월 10일
Does a drawnow after setting the color help?
Using a timer() would be smarter, because it does not block Matlab with lazy pause statements.
댓글 수: 2
Jan
2013년 7월 10일
Move from asnwer to comment section:
ferfer wrote: Thanks for your answer. " Drawnow " does not work , It changes the colour only when I press the button instead of automatically :(
Jan
2013년 7월 10일
@ferfer: Please post comments to answers in the corresponding section. Thanks.
Sorry, I did not read the code carefully enough. The change in the color appears in the button's callback. This callback is called, when the button is pressed. But why not changing the color directly:
while flag==0
check(estado) %%displays the value of the variable %%
estado=not(estado);
if boton_estado
set(handles.boton,'BackgroundColor','r');
else
set(handles.boton,'BackgroundColor','g');
end
pause(3) % This implies a DRAWNOW already!
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!