Problem when creating a datetime array using reference and sampling frequency
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello,
I am trying to create a datetime array that begins with a starting datetime (st) I've set and which increases with respect to a sampling frequency (fs). To do that I'm running this code
t=[];
t=st;
for i=2:length(time)
t(i)=t(i-1)+seconds(1/fs);
end
However, it seems that the program is stuck in an inifite loop. When running the loop step by step, it works fine.
I've also tried this:
t=[];
for i=1:length(time)
t=[t;st+ seconds(i/fs)];
end
But it still doesn't work.
댓글 수: 0
답변 (2개)
Steven Lord
2018년 7월 13일
It's going to be difficult to know why your code appears to run infinitely without knowing the contents of st, time, and fs. But if you know the start time, end time, and increment you want your vector to have, don't use a for loop (especially not one in which you grow the t array by one element each iteration!) Instead use the colon operator.
startTime = datetime('now');
endTime = startTime + days(1);
inc = hours(1);
V = startTime:inc:endTime;
This counts from right now to this time tomorrow in increments of 1 hour.
댓글 수: 0
yanis
2018년 7월 13일
댓글 수: 1
Peter Perkins
2018년 8월 3일
Following up on essentially the same idea that Steve suggested:
>> st = datetime(2018,8,3,16,0,0,'Format','dd-MMM-yyyy HH:mm:ss.SSS')
st =
datetime
03-Aug-2018 16:00:00.000
>> n = 10;
>> t = st + seconds((0:n-1)/125)
t =
1×10 datetime array
Columns 1 through 5
03-Aug-2018 16:00:00.000 03-Aug-2018 16:00:00.008 03-Aug-2018 16:00:00.016 03-Aug-2018 16:00:00.024 03-Aug-2018 16:00:00.032
Columns 6 through 10
03-Aug-2018 16:00:00.040 03-Aug-2018 16:00:00.048 03-Aug-2018 16:00:00.056 03-Aug-2018 16:00:00.064 03-Aug-2018 16:00:00.072
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!