MATLAB Answers

How to Execute statements in the script at scheduled time or defined date & time ?

1 view (last 30 days)
Hi all,
It would be great help if anybody can tell me How to Execute statements in the script at scheduled time or defined date & time with in the script. for example
T1 = datetime('now'); % T1 is current date & time
a= 10;
b= 20;
c = a*b;
T2 = T1 + hours(1) % T2 is after 1 hour of T1 time
% when the time difference is 1 hour the fallowing statements should execute
d = 20;
e = 20;
f = d*e;
I have tried with " Timer function " and " while " all possible ways and finally asking for your kind help.

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 4 Aug 2019
nelapati - if you just want your timer callback to fire x seconds from now, then using the examples from Create object to schedule execution of MATLAB commands, you could do something like
function myTimerTest
hTimer = timer('TimerFcn', @myTimerCallback, 'ExecutionMode', 'SingleShot', 'StartDelay', 10);
start(hTimer);
while strcmp(get(hTimer, 'Running'), 'on')
fprintf('timer is still runnning...\n');
pause(1.0);
end
delete(hTimer);
function myTimerCallback(hObject, event)
d = 20;
e = 20;
f = d*e
end
end
The above code is saved to a file called myTimerTest.m. It will execute the code in the myTimerCallback function 10 seconds after the timer is started (see the value assigned to the StartDelay property). If you want to fire a callback an hour from when the myTimeTest function is called, you would change this value to 3600.

  1 Comment

Sign in to comment.

More Answers (0)

Sign in to answer this question.