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)
Show older comments
Michael Hurley
on 6 Feb 2024
Edited: Cris LaPierre
on 7 Feb 2024
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'.
0 Comments
Accepted Answer
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']
inDateFormat= "'METRIC ALERT LOG - 'eee MMM dd HH:mm:ss yyyy";
dateTimeArray=datetime(charArray,'InputFormat',inDateFormat)
2 Comments
Cris LaPierre
on 7 Feb 2024
ah, I just forgot to update the code once I found the correct format. I'll update.
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!