matlab gui change color of tiles automatically

Hello everybody
I am writing an application in matlab gui. In my application, there are 6 boxes in order, I want these boxes to change color in a certain time science (for example, all boxes first turn black, then the 1st box turns red for 10 seconds, then turns off 10 seconds, all the boxes are black again, then the color of the 2nd box turns red for 10 seconds) What should I use to make the color change in this way regularly and timely? are there any sample codes or can you give an idea about what I should use?

1 Comment

You can use a timer object in MATLAB GUI to change the color of the boxes in a certain time sequence. You can set the timer's execution time, start and stop times, and callback function, which will be called periodically at the execution time.
Here's a sample code to give you an idea:
% Create a timer object t = timer;
% Set the timer's execution time to 0.5 seconds t.Period = 0.5;
% Set the timer's start and stop times t.StartFcn = @(x,y)disp('Timer started'); t.StopFcn = @(x,y)disp('Timer stopped');
% Define the timer's callback function t.TimerFcn = {@timer_callback, hObject};
% Start the timer start(t);
% Callback function for the timer function timer_callback(obj, event, hObject)
% Get the current time current_time = obj.TasksExecuted * obj.Period;
% Calculate the color of the boxes based on the current time if current_time < 10 box1_color = 'red'; box2_color = 'black'; elseif current_time < 20 box1_color = 'black'; box2_color = 'red'; else box1_color = 'black'; box2_color = 'black'; end
% Set the color of the boxes set(hObject.box1, 'BackgroundColor', box1_color); set(hObject.box2, 'BackgroundColor', box2_color);
end
Note: You'll need to adjust the code to handle the colors of all 6 boxes and the desired time sequence. Also, "hObject" is the handle to the GUI figure.

Sign in to comment.

Answers (1)

thank you very much but i got too many errors so i decided to continue with appdesigner.

Categories

Products

Release

R2022b

Asked:

Cat
on 1 Feb 2023

Answered:

Cat
on 2 Feb 2023

Community Treasure Hunt

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

Start Hunting!