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.

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'.

 Accepted Answer

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 Comments

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.
ah, I just forgot to update the code once I found the correct format. I'll update.

Sign in to comment.

More Answers (0)

Products

Release

R2023b

Tags

Community Treasure Hunt

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

Start Hunting!