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

2 次查看(过去 30 天)
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?

回答(1 个)

Peter Perkins
Peter Perkins 2020-4-27
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 个评论
Bailey Adams
Bailey Adams 2020-4-27
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)!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by