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 Center および File ExchangeClocks and Timers についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by