real clock seconds after 1 second delay in input

3 views (last 30 days)
max = 15
input = [0 0 0 0 1 1 1 0 0 0 1 1 1 1 1]
for i = 1:max
format shortg
c = clock
seconds = c(1,6)
end
% How can I get this input after every one second (one by one)?
% let say when I run my code at that time my clock second is 4.631 second of the clock.
% I mean I want output same like this in matrix formate
input time (actual seconds from the clock)
0 4.631
0 5.631
0 6.631
0 7.631
1 8.631
1 9.631
1 10.631
0 11.631
0 12.631
0 13.631
1 14.631
1 15.631
1 16.631
1 17.631
1 18.631
Thank you for your help...

Accepted Answer

Walter Roberson
Walter Roberson on 13 Aug 2020
If you do not need high precision on it being every 1.0 seconds elapsed, then:
max = 15;
input = [0 0 0 0 1 1 1 0 0 0 1 1 1 1 1];
for i = 1:max
c = clock;
seconds = c(1,6);
disp([input(i), seconds])
pause(1)
end
If you do need high precision, then you will need to create a timer() object with fixedRate execution mode
You could also:
max = 15;
input = [0 0 0 0 1 1 1 0 0 0 1 1 1 1 1];
T = tic;
for i = 1:max
c = clock;
seconds = c(1,6);
disp([input(i), seconds])
D = i+1-toc(T);
pause(D)
end
this is a more self-adjusting -- the later it gets in serving an input, the shorter the time it waits.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!