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

2 ビュー (過去 30 日間)
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 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 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)!

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by