MATLAB: Box plot days and years into months

boxplot

Hi,
I have a daily data A(244day x 63years) i.e A(244×63), I need to perform a monthly box plot, so I get the total amount in each month and boxplot these months in sequence starting from April to November, my starting point in my data is April till the end in November, so I get a new matrix of size A(8×63) then I boxplot it, where 8 is 8 months

Best Answer

I believe this does what you want, with a couple caveats.
YEARS_OF_DATA = 63;
MONTHSPERYEAR = 12;
% Date data -- will need to align this with the dates in your actual data
dateVector = datevec(datenum('2016-04-01'):datenum('2016-11-20'));
dateMonth = dateVector(:,2);
maxDateMonth = max(dateMonth);
numberDates = numel(dateMonth);
% Value data -- put your real data here.
A_daily = rand(numberDates,YEARS_OF_DATA);
% Initialize the monthly data
A_monthly = zeros(MONTHSPERYEAR,YEARS_OF_DATA);
for ny = 1:YEARS_OF_DATA
A_monthly(1:maxDateMonth,ny) = accumarray(dateMonth,A_daily(:,ny));
end
The caveats are that this is a bit more general than what you asked for, in that it will work if you have data over different specific dates. It will give a matrix that goes from January to the max month you have in your data. Any missing months (e.g. Jan-Mar and Dec in this code snippet), will have zeros. You can easily delete those rows if you want.