Repeating a day of the year sequence from a specified date input

2 visualizaciones (últimos 30 días)
Bailey Adams
Bailey Adams el 26 de Abr. de 2020
Comentada: Bailey Adams el 27 de Abr. de 2020
I'm trying to repeat a day of the year sequence in a for loop after the "day_num_index" reaches day 365. I want it to repeat the sequence (day 1 - day 365) and start over at day 1 after day 365 is reached. I need it to loop through for a specified number of iterations until the final index of the loop is reached. Additionally, the code should be compatible with any initial specified date (i.e. 1-Oct-2020 in the example provided below) throughout the year.
Anyone have some suggestions?

Respuestas (1)

Peter Perkins
Peter Perkins el 27 de Abr. de 2020
Right off the bat, 365 is a bug. I can't tell if you want to start at, say, 1-Oct-2020, and increment the day until you get to 31-Dec-2020 and wrap around to 1-Jan-2020 and keep going like that until you've had 1000 iterations, or if you want to increment the day until you get to 30-Sep-2021 and wrap around to 1-Oct-2020 and keep going like that until you've had 1000 iterations.
In either case, just let datetime do the incrementing and you do the modulo. Something like
startDate = datetime(2020,10,1);
wrapDate = dateshift(startDate,'start','year','next') - caldays(1);
% or wrapDate = startDate + calyears(1) - caldays(1);
d = startDate - caldays(1);
for i = 1:1000
if d == wrapDate
d = d - calyears(1);
end
d = d + caldays(1);
disp(d)
end
  1 comentario
Bailey Adams
Bailey Adams el 27 de Abr. de 2020
I'd like to start at any input date, increment that day until 31-Dec, and then wrap around to 1-Jan and continue for a specified number of iterations in the loop (like the first part of your statement)!

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by