How to execute a task every 20 minutes for 10 hours every day from 7:00 to 17:00

2 views (last 30 days)
So, I have this task where I have to run part of my code every 20 mintues for 10 hours every day from 7:00 (7am) to 17:00 (5pm). Any ideas how I would go about doing that? My initial idea is something like that:
lowerTime = datetime("09:00:00");
upperTime = datetime("17:00:00");
start_time = sprintf("%d:%d",lowerTime.Hour,lowerTime.Minute);
end_time = sprintf("%d:%d",upperTime.Hour,upperTime.Minute);
current_time = sprintf("%d:%d",datetime('now').Hour, datetime('now').Minute);
disp(current_time);
if strcmp(current_time, start_time)
run = true;
while run
% a while loop that runs for 10 hours
% possibly another while or a timer to execute every 20 mins
if strcmp(current_time,end_time)
run = false
end
end
But this sounds too complicated. Is there a better way to do that?

Answers (1)

Jon
Jon on 11 May 2022
You could use two timers.
The first timer would execute with a 24 hour period executing at 7:00AM each day. Its callback function would start up another timer that would execute a fixed number (10*60/20 = 30) of executions at 20 minute intervals. the callback for this timer would be the portion of code you want to execute every 20 minutes from 7:00 to 17:00

Community Treasure Hunt

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

Start Hunting!