MATLAB: Real clock seconds after 1 second delay in input

matlab guimatrix

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…

Best Answer

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.