Clear Filters
Clear Filters

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.

2 views (last 30 days)
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

Cris LaPierre
Cris LaPierre on 7 Feb 2024
Edited: Cris LaPierre on 7 Feb 2024
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
Michael Hurley
Michael Hurley on 7 Feb 2024
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!