How to change to different colors for a button in MATLAB in the middle of the program execution.

108 visualizaciones (últimos 30 días)
I want to change the color of a gui button in MATLAb in middle of the execution of the button that is I want to continously change the color of a button in program until a user presses it so that it is being highligted and the user will easily understand that a particular button has to be clicked.
  5 comentarios
dpb
dpb el 12 de Jun. de 2021
And you may need a drawnow to make the display refresh...ran into that in App Designer just recently when disabling/changing color on a button during execution of the associated code...
Adam Danz
Adam Danz el 12 de Jun. de 2021
Editada: Adam Danz el 14 de Jun. de 2021
@SUMAN DUTTA The solution in my answer isn't too much different than then one you commented on earlier. That one changes the visible property, this one changes the BackgroundColor property.

Iniciar sesión para comentar.

Respuestas (1)

Adam Danz
Adam Danz el 12 de Jun. de 2021
Editada: Adam Danz el 12 de Jun. de 2021
> I want to continously change the color of a button in program until a user presses it
An efficient method that will work in the background and will not interfere with ongoing processes or user interaction is to use a timer object.
The two demos below create a figure with a uibutton. It's a state button but can easily be adapted to work with any app component.
The timer changes the background color of the button every 0.2 seconds (200 ms). That period can easily be changed in the timer function. You can also alternate between a pre-defined list of colors instead of flashing random colors (demo #2).
When the button is pressed (state change from 0 to 1), the button background is set to default (gray). When the button is pressed again (state change from 0 to 1) the randomized colors start again.
You can start and stop the random colors any time you want using start(app.uibTimer) and stop(app.uibTimer).
% Create app with 1 button
app.uifigure = uifigure();
app.uibutton = uibutton(app.uifigure,'state',...
'Text','Stop the madness',...
'Value',false, ...
'Position', [100 100 150 80], ...
'FontSize', 16, ...
'FontWeight','bold');
% Create timer to set the button color to random colors every 200ms
app.uibTimer = timer('Period',.2, 'ExecutionMode','fixedDelay',...
'TimerFcn',@(~,~)set(app.uibutton, 'BackgroundColor', rand(1,3)));
% Define other functions
app.uibutton.DeleteFcn = @(~,~)delete(app.uibTimer);
app.uibutton.ValueChangedFcn = @(btn,evt)stateButtonPressed(app,btn,evt); % After creating timer!
start(app.uibTimer)
% Function that responds to button press
function stateButtonPressed(app, btnHandle, event)
if event.Value
% Button is TRUE: set color to default
btnHandle.BackgroundColor = [.96 .96 .96]; % default color
stop(app.uibTimer)
else
% Button is FALSE: Turn on random colors
start(app.uibTimer)
end
end
Demo 2: alternates between two colors
Choose a base color and the timer will alternate between the base and it's opposite defined by 1-rgb where rgb is the 1x3 color value of the base color.
% Create app with 1 button
app.uifigure = uifigure();
app.uibutton = uibutton(app.uifigure,'state',...
'Text','Stop the madness',...
'Value',false, ...
'Position', [100 100 150 80], ...
'FontSize', 16, ...
'FontWeight','bold');
% Define the base color (1x3 vector) and create timer to
% set the button color to alternative the base and its
% opposite color every 200ms
app.uibutton.BackgroundColor = [1 1 0];
app.uibTimer = timer('Period',.2, 'ExecutionMode','fixedDelay',...
'TimerFcn',@(~,~)set(app.uibutton, 'BackgroundColor', 1-app.uibutton.BackgroundColor));
% Define other functions
app.uibutton.DeleteFcn = @(~,~)delete(app.uibTimer);
app.uibutton.ValueChangedFcn = @(btn,evt)stateButtonPressed(app,btn,evt); % After creating timer!
start(app.uibTimer)
% Function that responds to button press
function stateButtonPressed(app, btnHandle, event)
if event.Value
% Button is TRUE: set color to default
btnHandle.BackgroundColor = [.96 .96 .96]; % default color
stop(app.uibTimer)
else
% Button is FALSE: Turn on flashing colors
btnHandle.BackgroundColor = [1 1 0]; %base color
start(app.uibTimer)
end
end

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by