Remove leading zeros from dates strings
25 views (last 30 days)
Santiago Costantino on 16 Jul 2020
I need to find the number of times a set of dates is included in a large text file. The problem is that the text file does not use leading zeros for months and days.
My code should look something like this:
numberOfTimes(it)=size(regexp(myText, datestr(dayOne+days(it), 'mm/dd/yy')), 2);
jonas on 16 Jul 2020
You can use histogram() to count occurences, either with datetime or categoricals as input.
dates = datetime(2010,1,12) + rand(1000,1)*days(365);
% with categoricals
dates_cell = cellstr(datestr(dates,'mm/dd/yy'));
dates_cat = categorical(dates_cell);
H = histogram(dates_cat)
More Answers (1)
Steven Lord on 16 Jul 2020
Use M instead of MM and d instead of dd in the input format. According to the description of the Format property on the documentation page (which is referenced in the documentation for 'InputFormat'), M handles "Month, numerical using one or two digits" as opposed to MM which handles "Month, numerical using two digits".
>> datetime('07-16-2020', 'InputFormat', 'M-dd-yyyy')
>> datetime('7-16-2020', 'InputFormat', 'M-dd-yyyy')