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

조회 수: 2 (최근 30일)
John Doe
John Doe 2022년 5월 11일
답변: Jon 2022년 5월 11일
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?

답변 (1개)

Jon
Jon 2022년 5월 11일
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

카테고리

Help CenterFile Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by