Update Background colour Button

1 view (last 30 days)
ferfer
ferfer on 10 Jul 2013
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!

Answers (2)

Jan
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
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
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

Sign in to comment.


ferfer
ferfer on 12 Jul 2013
It still does not work :S . I have written:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global estado
global flag
boton;
flag=0;
estado=0;
while flag==0
check(estado)
estado=not(estado);
if boton_estado==0
set(handles.boton,'BackgroundColor','r');
pause(3) % This implies a DRAWNOW already!
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
And then:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function varargout = boton(varargin)
% BOTON MATLAB code for boton.fig
% BOTON, by itself, creates a new BOTON or raises the existing
% singleton*.
%
% H = BOTON returns the handle to a new BOTON or the handle to
% the existing singleton*.
%
% BOTON('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BOTON.M with the given input arguments.
%
% BOTON('Property','Value',...) creates a new BOTON or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before boton_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to boton_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help boton
% Last Modified by GUIDE v2.5 10-Jul-2013 12:52:22
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @boton_OpeningFcn, ...
'gui_OutputFcn', @boton_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before boton is made visible.
function boton_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to boton (see VARARGIN)
% Choose default command line output for boton
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes boton wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = boton_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- 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;
% --- Executes on button press in salida.
function salida_Callback(hObject, eventdata, handles)
% hObject handle to salida (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global flag
flag=1;
close
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
When I run the script, Matlab displays: "Error in a (line 11) Undefined variable "handles" or function "handles.boton. set(handles.boton,'BackgroundColor','r');"
Thank you so much for your help!!!

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!