real clock seconds after 1 second delay in input

조회 수: 2 (최근 30일)
Vivek Shukla
Vivek Shukla 2020년 8월 13일
답변: Walter Roberson 2020년 8월 13일
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...

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 13일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Clocks and Timers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by