WEEKOFYEAR Ordinal week number. WEEKOFYEAR(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND) returns the ordinal week number in the given year plus a fractional part depending on the day and time of day. [WEEK, YEAR] = WEEKOFYEAR(...) also returns the year the week belongs to. The week number is an integer between 1 and 53, inclusive. Any missing MONTH or DAY will be replaced by ones. Any missing HOUR, MINUTE or SECOND will be replaced by zeros. If no date is specified, the current date and time is used. This function is ISO 8601 compliant: The first week of a given year is the first week which has more days in the given year than in the previous year.

- dayofweek DAYOFWEEK Day of week.
- dayofyear DAYOFYEAR Ordinal number of day in a year.
- weeksinyear WEEKSINYEAR Number of weeks in a year.

0001 function [w, y] = weekofyear(varargin) 0002 %WEEKOFYEAR Ordinal week number. 0003 % 0004 % WEEKOFYEAR(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND) returns the 0005 % ordinal week number in the given year plus a fractional part 0006 % depending on the day and time of day. 0007 % 0008 % [WEEK, YEAR] = WEEKOFYEAR(...) also returns the year the week belongs 0009 % to. 0010 % 0011 % The week number is an integer between 1 and 53, inclusive. 0012 % 0013 % Any missing MONTH or DAY will be replaced by ones. Any missing HOUR, 0014 % MINUTE or SECOND will be replaced by zeros. If no date is specified, 0015 % the current date and time is used. 0016 % 0017 % This function is ISO 8601 compliant: The first week of a given year is 0018 % the first week which has more days in the given year than in the 0019 % previous year. 0020 0021 % Author: Peter J. Acklam 0022 % Time-stamp: 2002-03-03 12:49:15 +0100 0023 % E-mail: pjacklam@online.no 0024 % URL: http://home.online.no/~pjacklam 0025 0026 nargsin = nargin; 0027 error(nargchk(0, 6, nargsin)); 0028 if nargsin 0029 argv = { 1 1 1 0 0 0 }; 0030 argv(1:nargsin) = varargin; 0031 else 0032 argv = num2cell(clock); 0033 end 0034 [year, month, day, hour, minute, second] = deal(argv{:}); 0035 0036 % ISO 8601 states that a week that lies partly in one year and partly 0037 % in another is assigned a number in the year in which most of its 0038 % days lie. Consequence: Week 1 of any year is the week that 0039 % contains 4 January. Hence, the first week started on the following 0040 % day of the year 0041 % 0042 % yd1 = 4: week 1 started on 4 January of current year 0043 % yd1 = 3: week 1 started on 3 January of current year 0044 % yd1 = 2: week 1 started on 2 January of current year 0045 % yd1 = 1: week 1 started on 1 January of current year 0046 % yd1 = 0: week 1 started on 31 December of previous year 0047 % yd1 = -1: week 1 started on 30 December of previous year 0048 % yd1 = -2: week 1 started on 29 December of previous year 0049 % 0050 yd1 = 5 - dayofweek(year, 1, 4); 0051 0052 % Get the ordinal day number and calculate the "raw" week number. 0053 yd2 = dayofyear(year, month, day); 0054 w = 1 + floor( (yd2 - yd1)/7 ); 0055 0056 % Now, if the week number is 0, the week is either week 52 or 53 of 0057 % the previous year. If the week number is 53, the week is either 0058 % week 53 of the current year or week 1 of the next year. 0059 0060 y = year; 0061 if length(y) == 1, y = y(ones(size(w))); end; % scalar expansion 0062 0063 % Take care of the case when the week number is 0. 0064 i = find(w == 0); 0065 if ~isempty(i) 0066 y(i) = y(i) - 1; 0067 w(i) = weeksinyear(y(i)); 0068 end 0069 0070 % Take care of the case when the week number is 53. 0071 i = find(w == 53); 0072 if ~isempty(i) 0073 j = find(weeksinyear(y(i)) == 52) 0074 if any(j(:)) 0075 y(i(j)) = y(i(j)) + 1; 0076 w(i(j)) = 1; 0077 end 0078 end 0079 0080 % Now add the fractional part of the day. 0081 w = w + ( 86400*( dayofweek(year, month, day) - 1 ) ... 0082 + 3600*hour + 60*minute + second )/604800;

Generated on Mon 15-Sep-2014 13:31:28 by