# Help Optimize slow Datestr() function performance

조회 수: 2 (최근 30일)
Brian 2014년 1월 17일
댓글: Brian 2014년 1월 20일
I have imported a large text file with textual dates in the format m/d/yyyy. The end result that I need is a numeric date (for export to another system) in the format yyyymmdd. Since my text file is rather large (850,000 rows) my current method for conversion is really slow. It's taking about 50 seconds for the single line of code to run and is about 75% of my code's total run time. Since I have to run this code iteratively at times I'm interested in any suggestions to optimize how I'm using datestr, or an alternative.
Here's my code
tic
MyDates = str2num(datestr(MyData{1,2},'yyyymmdd'));
toc
yields - Elapsed time is 49.348078 seconds.
##### 댓글 수: 2없음 표시없음 숨기기
José-Luis 2014년 1월 17일
Please show how MyData{1,2} looks like.
Brian 2014년 1월 17일
{'12/31/1989';'1/31/1990';'2/28/1990';'3/31/1990'};
850,000 rows of a cell containing date text. It would be a little easier if the month came in the format 'mm', but it does not. For months 1-9, there is just a single number.

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

### 채택된 답변

Walter Roberson 2014년 1월 17일
t = datevec(MyData{1,2});
MyDates = t(:,1) * 10000 + t(:,2) * 100 + t(:,3);
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
Walter Roberson 2014년 1월 17일
Out of interest, how long does it take now? Comparing to the 50 seconds.
Brian 2014년 1월 20일
Ran it three times and now takes between 5.5 and 6.5 seconds. Again, thanks a lot. That's a dramatic improvement.

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

### 카테고리

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