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!