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

조회 수: 2 (최근 30일)
Bailey Adams
Bailey Adams 2020년 4월 26일
댓글: Bailey Adams 2020년 4월 27일
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