How to take an average by date

13 views (last 30 days)
a p
a p on 20 Mar 2018
Answered: Peter Perkins on 23 Mar 2018
I have a set of files, each with data from a trial of an experiment. I need to run a function on each file to summarize the data from each file (in a percentage). I then need to group the files by date (of the data collection) and average the percentages for each day, so that I have one total percentage for each day of data. I load the files using 'dir' and I have the function written to get the percentage in each file, but I cannot figure out how to group the files by date to take the average of each day. I eventually want to be able to plot the data with one point for each day that I have files from. Any help would be appreciated!

Answers (2)

Peter Perkins
Peter Perkins on 23 Mar 2018
Put your data in a timetable, then use retime, with 'mean'. It's one line:
>> tt = timetable(datetime(2018,3,23,1:12:71,0,0)',rand(6,1))
tt =
6×1 timetable
Time Var1
____________________ _______
23-Mar-2018 01:00:00 0.95717
23-Mar-2018 13:00:00 0.48538
24-Mar-2018 01:00:00 0.80028
24-Mar-2018 13:00:00 0.14189
25-Mar-2018 01:00:00 0.42176
25-Mar-2018 13:00:00 0.91574
>> ttMean = retime(tt,'daily','mean')
ttMean =
3×1 timetable
Time Var1
____________________ _______
23-Mar-2018 00:00:00 0.72127
24-Mar-2018 00:00:00 0.47108
25-Mar-2018 00:00:00 0.66875

KSSV
KSSV on 20 Mar 2018
You have dates in hand.......convert the dates into datevec. From these you can sort them. Read about datevec.
  2 Comments
a p
a p on 20 Mar 2018
so after using datevec here is what i got:
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 3 12
2018 3 12
2018 3 12
2018 3 12
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
How do I group my files according to date in order to average the values for each day?
KSSV
KSSV on 21 Mar 2018
Edited: KSSV on 21 Mar 2018
clc; clear all ;
dates = [ 2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 2 9
2018 3 12
2018 3 12
2018 3 12
2018 3 12
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13
2018 3 13] ;
%
months = dates(:,2) ;
dayes = dates(:,3) ;
[c,ia,ib] = unique(months) ;
iwant = cell(length(c),1) ;
for i = 1:length(c)
iwant{i} = dates(ib==i,:) ;
iwant{i}
end

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!