Pause code execution to achieve desired execution rate
Run Loop at Fixed Rate
Create a rate object that runs at 1 Hz.
r = rateControl(1);
Start a loop using the
rateControl object inside to control the loop execution. Reset the object prior to the loop execution to reset timer. Print the iteration and time elapsed.
reset(r) for i = 1:10 time = r.TotalElapsedTime; fprintf('Iteration: %d - Time Elapsed: %f\n',i,time) waitfor(r); end
Iteration: 1 - Time Elapsed: 0.002921 Iteration: 2 - Time Elapsed: 1.001767 Iteration: 3 - Time Elapsed: 2.001443 Iteration: 4 - Time Elapsed: 3.001479 Iteration: 5 - Time Elapsed: 4.002085 Iteration: 6 - Time Elapsed: 5.001422 Iteration: 7 - Time Elapsed: 6.001002 Iteration: 8 - Time Elapsed: 7.001471 Iteration: 9 - Time Elapsed: 8.001282 Iteration: 10 - Time Elapsed: 9.001391
Each iteration executes at a 1-second interval.
numMisses — Number of missed task executions
Number of missed task executions, returned as a scalar.
waitfor returns the number of times the task was missed in
Rate object based on the
LastPeriod time. For example, if the desired rate is
1 Hz and the last period was 3.2 seconds,
Introduced in R2016a