0
votes

My data is excel column. In excel sheet one column contain last 50 years date (no missing date; dd/mm/yyyy format) and in other column everyday rainfall (last 50 years; no blank).

I want to calculate what is the sum of monthly rainfall for every month of last 50 years in Matlab. Remember, there four types of month ending date: 30, 31, 28 and 29. Upto now I am able read the dates and rainfall value from excel file like below

filename = 'rainfalldate.xlsx';

% Extracts the data from each column of textData

[~,DateString ]= xlsread(filename,'A:A')

formatIn = 'dd/mm/yyyy';

DateVector = datevec(DateString,formatIn)

rainfall = xlsread(filename,'C:C');

what is the next step so that I can see every months of last fifty years rainfall sum?

I mean suppose July/1986 rainfall sum... Any new solutions?Code or loop base in Matlab 2014a

1
but you already converted it into a date vector, all you have to do now is to index the correct year and month (you can do this using find) and then sum it up...GameOfThrows
Take a look at accumarrayDaniel

1 Answers

0
votes

As @Daniel suggested, you could use accumarray for this.

Since you want the monthly rainfall for multiple years, you could combine year and month into one single variable, ym.

It is possible then to identify if a value belongs to both a determined year and month at once, and "tag" it using histc function. Values with similar "tags" are summed by the accumarray function.

As a bonus, you do not have to worry about the number of days in a month. Take a look at this example:

% Variables initialization
y=[2014 2014 2015 2015 2015 2014 2014 2015 2015];
m=[1 1 1 2 2 3 3 3 3]; % jan, feb and march
values = 1:9;

% Identifyier creation and index generation
ym = y*100+m;
[~,subs]=histc(ym,unique(ym));

% Sum
total = accumarray(subs',values);

The total variable is the monthly rainfall already sorted by year and month (yyyymm).