I am trying to write a programme in MATLAB to calculate the average monthly rainfalls of a large data set I have. I input data is in the form of a column of rainfall values for a number of years. The rainfall is collected on a daily basis but I am only interested in the average monthly rainfall. I need a programme to calculate this for me as it would take too long manually. This is what i have so far:

%Specify the number of columns of input data

col=1;

%Open the input file

fid = fopen('sum.txt','r');

X = fscanf(fid,'%g %g',[col inf]);

X = X';

fclose(fid);

%Define initial conditions

i=1989;

%current day

k=1;

%current month of year

j=0;

%months

p=0;

%number of days in month

D=0;

%Outer loop counts the years

while i<=1999;

i=i+1;

%Inner loop counts the months

while j<=12;

j=j+1; p=p+1; k=k+D;

%Determine the number of days in each month

if j==1 || j==3 || j==5 || j==7 || j==8 || j==10 || j==12

D=31;

%Determine if year is a leap year or not

else

if j==2 && rem(i,4)==0 && rem(i,100)~=0 || rem(i,400)==0

D=29;

else

D=28;

if j==4 || j==6 ||j==9 || j==11

D=30;

end

end

end

end

end

%Calculate average monthly rainfalls

average(p) = (X(k) + X(k+D-1))/D

it just does not work and I am really stuck.

# Calculate average monthly rainfall in matlab

Page 1 of 1## 3 Replies - 16819 Views - Last Post: 10 February 2009 - 08:03 PM

##
**Replies To:** Calculate average monthly rainfall in matlab

### #2

## Re: Calculate average monthly rainfall in matlab

Posted 09 February 2009 - 06:10 PM

Can you post a sample of the data set, like ten rows?

### #3

## Re: Calculate average monthly rainfall in matlab

Posted 10 February 2009 - 02:59 AM

### #4

## Re: Calculate average monthly rainfall in matlab

Posted 10 February 2009 - 08:03 PM

Now, I don't have MATLAB on my computer, and, to be honest, nor have I ever used MATLAB, however I think the logic of this code should be clear enough for you to implement it yourself:

Keep in mind, though, that I do not have MATLAB, so I never tested this code. Therefore, you may recieve some syntax errors.

function l = isleapyear(year) % isleapyear(year) Returns 1 if the specified year % is a leap year % Is the year divisible by four? if rem(year, 4) == 0 l = 1 % Then the year is a leap year % Is the year a centenary year and is it divisible by 400? if (rem(year, 100) == 0 && rem(year, 400) == 0) l = 0; % Then the year isn't a leap year end else l = 0; % Not a leap year end function d = daysinmonth(month, year) % daysinmonth(month, year) Returns the number of days in % the given month of the given % year. The month is represented % by an integer between 1 and 12 % where 1 = Jan, 2 = Feb, 3 = Mar, % etc. if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) % January, March, May, June, July, August, d = 31; % October, and December all have 31 days elseif ((month == 4) || (month == 6) || (month == 9) || (month == 11)) % April, June, September, and November d = 30; % have 30 days elseif month == 2 if leapyear(year) % February has 29 days if it is a leap year d = 29; else % Otherwise, it has 28 days d = 28; end fid = fopen('sum.txt', 'r'); if(fid < 0) error('Couldn''t open sum.txt!'); end X = fscanf(fid, '%f'); fclose(fid); startmonth = 1; % The month of the first rainfall measurement in the data set startyear = 1989; endmonth = 8; % The month whose rainfall average we want to calculate endyear = 1990; % In this case, we want the average rainfall of August 1990 days = 0; % Days since startmonth of startyear month = startmonth; year = startyear; done = 0; % Determines if we should keep looping while year <= endyear && ~done while(month <= 12) if (month == endmonth) && (year == endyear) done = 1; break end days = days + daysinmonth(month, year); month = month + 1; end if ~done month = 1; year = year + 1; end end if done average = mean(X(days : days + (daysinmonth(endmonth, endyear) - 1))); end

Keep in mind, though, that I do not have MATLAB, so I never tested this code. Therefore, you may recieve some syntax errors.

Page 1 of 1