MATLAB: How to i generate monthly sequence

monthly loop

I will like to generate number of days for each month in a calendar year by looping from January to December. The output of each monthly data will be saved. I want run the loop for all the 365 days in a year but have output for each month. For example: daystart=[1,32,60,91,121,152,182,213,244,274,305,335] and dayend = daystart + ndays.per.month(31 28 31 30 31 30 31 31 30 31 30 31).

Best Answer

You don't need to use for-loop to obtain number of days for each month. The following example generates number of days per month from Jan. to Dec. in 2017.
t = datetime(2017,1,1):calmonths(1):datetime(2018,1,1);
daysPerMonth = days(diff(t));
The result is:
>> daysPerMonth
daysPerMonth =
31 28 31 30 31 30 31 31 30 31 30 31