Update Background colour Button
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Jan
on 10 Jul 2013
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 Comments
Jan
on 10 Jul 2013
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
on 10 Jul 2013
@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
See Also
Categories
Find more on Loops and Conditional Statements 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!