How to change to different colors for a button in MATLAB in the middle of the program execution.
70 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2021-6-12
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
2021-6-12
编辑:Adam Danz
2021-6-14
@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.
回答(1 个)
Adam Danz
2021-6-12
编辑:Adam Danz
2021-6-12
> 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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!