How to pull dates out of character array, to get a datetime array. The character array was created from a log file in txt format.

조회 수: 3 (최근 30일)
This is related to this Q&A. Seems like I am close. Any help is apprecaited.
%Pull out dates
matchStr= 'METRIC ALERT LOG';
charArray= char(lines2023(contains(lines2023,matchStr)))
charArray = 89×43 char array
'METRIC ALERT LOG - Sun Jan 1 13:15:20 2023'
'METRIC ALERT LOG - Sun Jan 1 21:30:20 2023'
...
'METRIC ALERT LOG - Thu Dec 28 12:00:20 2023'
'METRIC ALERT LOG - Thu Dec 28 12:15:20 2023'
inDateFormat= "'METRIC ALERT LOG - *** 'MM dd hh:mm:ss yyyy";
dateTimeArray=datetime(charArray(:,:),'InputFormat',inDateFormat); % convert date strings to datetime
Error using datetime
Unable to convert the text to datetime using the format ''METRIC ALERT LOG - *** 'MM dd hh:mm:ss yyyy'.

채택된 답변

Cris LaPierre
Cris LaPierre 2024년 2월 7일
편집: Cris LaPierre 2024년 2월 7일
charArray = ['METRIC ALERT LOG - Sun Jan 1 13:15:20 2023'
'METRIC ALERT LOG - Sun Jan 1 21:30:20 2023'
'METRIC ALERT LOG - Thu Dec 28 12:00:20 2023'
'METRIC ALERT LOG - Thu Dec 28 12:15:20 2023']
charArray = 4x43 char array
'METRIC ALERT LOG - Sun Jan 1 13:15:20 2023' 'METRIC ALERT LOG - Sun Jan 1 21:30:20 2023' 'METRIC ALERT LOG - Thu Dec 28 12:00:20 2023' 'METRIC ALERT LOG - Thu Dec 28 12:15:20 2023'
inDateFormat= "'METRIC ALERT LOG - 'eee MMM dd HH:mm:ss yyyy";
dateTimeArray=datetime(charArray,'InputFormat',inDateFormat)
dateTimeArray = 4x1 datetime array
01-Jan-2023 13:15:20 01-Jan-2023 21:30:20 28-Dec-2023 12:00:20 28-Dec-2023 12:15:20
  댓글 수: 2
Michael Hurley
Michael Hurley 2024년 2월 7일
Thanks much!
You probably knwo, but I did not need to use the inDateFormat line at all with the input format correction you made in the following line. Thanks also for the quick reply.
Cris LaPierre
Cris LaPierre 2024년 2월 7일
ah, I just forgot to update the code once I found the correct format. I'll update.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by