Remove leading zeros from dates strings

조회 수: 13 (최근 30일)
Santiago Costantino
Santiago Costantino 2020년 7월 16일
댓글: Santiago Costantino 2020년 7월 17일
Hello
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:
myText=fileread('largeFile.txt');
dayOne=datetime('06/05/20','InputFormat','MM/dd/yy')
for it=1:10
numberOfTimes(it)=size(regexp(myText, datestr(dayOne+days(it), 'mm/dd/yy')), 2);
end
Thanks
  댓글 수: 1
jonas
jonas 2020년 7월 16일
편집: jonas 2020년 7월 16일
Why not simply count the dates with hist?

댓글을 달려면 로그인하십시오.

채택된 답변

jonas
jonas 2020년 7월 16일
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)
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 7월 17일
You could use mm and then use regexp to strip out leading 0. But it hardly seems worth it considering that datetime() can handle the situation
dates = datetime(2010,1,12) + rand(10,1)*days(365);
dates.Format = 'M/d/yy';
dates_cat = categorical(cellstr(dates));
histogram(dates_cat);
Santiago Costantino
Santiago Costantino 2020년 7월 17일
That does it. Thank you, Walter.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 7월 16일
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')
ans =
datetime
16-Jul-2020
>> datetime('7-16-2020', 'InputFormat', 'M-dd-yyyy')
ans =
datetime
16-Jul-2020
  댓글 수: 1
Santiago Costantino
Santiago Costantino 2020년 7월 16일
편집: Santiago Costantino 2020년 7월 16일
Sorry, I guess my question was not clear enough.
The dates in the text file are written as '4/7/20' for April 7th 2020. I need to find the number of occurrences of each one of many different and consecutive dates in the text.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by