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?
0 个评论
回答(1 个)
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
另请参阅
类别
在 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!