How to plot event frequency using datetime array

10 views (last 30 days)
Hello, I have an array of values in the datetime format. Each value represent an event happening at the specified date and time. How can I plot event frequency in events per day, events per month, etc?
I already managed to plot events per hour of the day using histogram(mydata.Hour)
Thank you for your answers!
EDIT: some clarification I don't want to plot "how many events happened on Mondays", or "how many events happened in January". I know I can do that by calling Month or Day properties of datetime. What I want to do is plot the mean number of events per a period of time . For example, how many events per day, over the whole range of dates.
here is what I got so far:
% generating 500 random events
dates = datetime(now-1000*rand(500,1),'convertfrom','datenum');
figure;
edges = -0.5:23.5;
histogram(dates.Hour,edges)
title('Events per hours of the day')
xlim ([-0.5 23.5])
ax1 = gca;
ax1.XTick = 0:2:23;
ax1.XTickLabel = {'Midnight','2','4','6','8','10','Noon','14','16','18','20','22'};
ax1.XTickLabelRotation = 45;
figure;
daynumber = weekday(dates);
histogram(daynumber)
title('Events per days of the week')
ax2 = gca;
ax2.XTick = [1:7];
ax2.XTickLabel = {'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'};
ax2.XTickLabelRotation = 45;

Answers (3)

Walter Roberson
Walter Roberson on 13 Jun 2015
histogram(mydata.Day), histogram(mydata.Month)
This assumes that you want all events on the same relative day number to be treated the same no matter which month they are, and that you want all events on the same relative month to be treated the same no matter which year they are. If you wanted to do something like histogram for each day of the year, you would want
histogram(day(mydata))
and to count by month over a period of years,
histogram(mydata.Year * 12 + mydata.Month)
  1 Comment
Manu Nico
Manu Nico on 13 Jun 2015
Thanks! However I already know how to do that. I edit the question with some clarification.

Sign in to comment.


Geoffrey Forden
Geoffrey Forden on 8 Feb 2017
I think this might work: convert your date into a date number (use dnumb=datenum(date), which counts the days from some start time that Mathworks uses as a fixed date) and then histogram it. You can pick your interval by the bins option in HIST. This may not look pretty because the dates start at something like 730,000. But you can use relative dates by just subtracting the lowest date you have.

Peter Perkins
Peter Perkins on 8 Feb 2017
I'm not sure this is right, but it sounds like you want, for example, to count the number of events on each day, and then take the mean of those counts over all days. A scalar. If that's correct, try this
dates = datetime - days(1000*rand(500,1)); % avoid datenums
bins = discretize(dates,'day')
mean(histcounts(bins))
You can actually do this just by calling histcounts on dates, but discretize has that nice 'day' behavior that figures out where the edges need to be. To call histcounts, you'll need to create a datetime vector using something like
datetime(2015,1,1) + days(0:1000)
or whatever the desired range is.

Community Treasure Hunt

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

Start Hunting!