Problem when creating a datetime array using reference and sampling frequency
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Steven Lord
el 13 de Jul. de 2018
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 comentarios
yanis
el 13 de Jul. de 2018
1 comentario
Peter Perkins
el 3 de Ag. de 2018
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
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!