real clock seconds after 1 second delay in input

2 visualizaciones (últimos 30 días)
Vivek Shukla
Vivek Shukla el 13 de Ag. de 2020
Respondida: Walter Roberson el 13 de Ag. de 2020
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...

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Ag. de 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.

Más respuestas (0)

Categorías

Más información sobre Clocks and Timers en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by