MATLAB Answers

0

Find monthly mean from daily data of same year using find function

Asked by Naveen Mk on 10 Jul 2019
Latest activity Commented on by KSSV
on 10 Jul 2019
Accepted Answer by KSSV
Hi I have a spreadsheet contains 1 year data and I want to find monthly mean column1 contains dd-mmm-yyyy And column2 contains data

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by KSSV
on 10 Jul 2019
 Accepted Answer

REad about datevec. This will give you years, months and days. Using the months and your data you can find mean. Let thedates be your dates and A be your data.
[y,m,d] = datevec(thedates) ;
iwant = zeros(12,1) ;
for i = 1:12
iwant(i) = mean(A(m==i)) ;
end

  4 Comments

Show 1 older comment
Guillaume Yes I agree, we have lot of functions to utilize. Most of the users here are beginners and they ask question to learn (I assume,:)). I gave a basic answer to show them how this can be done, so that they can learn.
Well, I'm sorry to say but you're teaching beginners bad practices. As I said, your code will not work properly if the there is more than one year in the data.
And your code cannot be easily changed to aggregate the data per quarter or per week. Using retime, it's as trivial as specifying 'quarterly' or 'weekly' instead of 'monthly'. And the intent is of course much clearer.
As the use said..the data is of one year.....quickly I gave that....

Sign in to comment.


Answer by Guillaume
on 10 Jul 2019

The easiest way:
data = readtimetable('C:\somewhere\somefile.xlsx'); %may need some adjustement depending on the actual format of the line
monthlydata = retime(data, 'monthly', 'mean');
Done!

  0 Comments

Sign in to comment.