MATLAB Answers

0

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

Asked by nelapati dhana on 26 Jul 2019
Latest activity Commented on by nelapati dhana on 13 Aug 2019
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.

1 Answer

Answer by Geoff Hayes
on 4 Aug 2019
 Accepted Answer

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

Thank you for your answer Geoff Hayes... I have solved the problem with timer function .

Sign in to comment.