Not quite sure what you are asking for here.
I assume you have a vector with all days from Monday-Sunday, and you know which day the first day of the year is:
weekdays = [{'Monday'},{'Tuesday'},{'Wednesday'},{'Thursday'},{'Friday'},{'Saturday'},{'Sunday'}];
dayNumber = zeros(1,12);
dayNumber(1) = 2;
numDays = [31 28 31 30 31 30 31 31 30 31 30 31];
for i = 1:11
dayNumber(i+1) = mod((dayNumber(i)+days(i)-1),7)+1;
end
for i = 1:12
dayName(i) = weekdays(dayNumber(i));
end
If you need to account for leap years: It is a leap year every fourth year, except years that can be divided by 100, except years that can be divided by 400.
A general approach would be something like this:
year = 2013;
year0 = 1900;
startDay = 1;
if year > year0
for i = 1:(year-year0)
if mod((year0+i),4)~=0
startDay = startDay + 365;
elseif mod((year0+i),4)==0 && mod((year0+i),100)~=0
startDay = startDay + 366;
elseif mod((year0+i),4)==0 && mod((year0+i),100)==0 && mod((year0+i),400)~=0
startDay = startDay + 365;
elseif mod((year0+i),4)==1 && mod((year0+i),400)==0
startDay = startDay + 366;
end
end
end
dayNumber = mod(startDay,7)+1;
This gives dayNumber = 2, for year = 2013. Then continue with the code above. (numDays(2)=29, if leap year)
Best Answer