Counting days by month

5 views (last 30 days)
dan berkowitz
dan berkowitz on 22 Oct 2017
Commented: Andrei Bobrov on 23 Oct 2017
hi,
i have a 100x11 char array with dates (e.g. '03-Jan-2006'). how can i create an array with the number of days in each unique month that appear in my date array. (e.g. if i have two days in january 2006, then i want it to return 2). any help would be appreciated. thx in advance.
DB

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 22 Oct 2017
Edited: Andrei Bobrov on 22 Oct 2017
Here A - your array "100x11 char array with dates (e.g. '03-Jan-2006')"
z = datetime(A);
T = table(datetime(z,'F','MMM-uuuu'),eomday(year(z),month(z)),'v',{'date','days'});
  4 Comments
dan berkowitz
dan berkowitz on 23 Oct 2017
Thank you! I didn't know about the ymd function! Appreciate your help.
Andrei Bobrov
Andrei Bobrov on 23 Oct 2017
[y,m] = ymd(z);
or
[y,m] = datevec(z);

Sign in to comment.

More Answers (1)

KL
KL on 22 Oct 2017
Edited: KL on 22 Oct 2017
It's better to change your char array into datetime vector
dt_array = datetime(char_array,'InputFormat','dd-MMM-yyyy')
and then you can use
numOfDays = nnz(dt_array.Month==your_month)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!