MATLAB Answers


Read Date time series format

Asked by sriram shastry on 17 Aug 2019 at 15:51
Latest activity Edited by per isakson
on 14 Sep 2019 at 9:03
I want to read Date and time series from below format
% file.txt has time formatted data
% My Code is as follows
VariableNames = {'Date'};
VariableWidths = [20]
VariableTypes = {'datetime'};
opts = fixedWidthImportOptions('VariableNames','SelectedVariableNames', [1]);
opts = setvaropts(opts, 'Date', 'InputFormat', 'MMdd/HHmmss.SSSSSS');
Content.update_engine = readtable('file.txt', opts);
I am getting following error as I am unable to read the time properly.
Warning: Unable to convert one or more variables to datetime. Specify the correct
InputFormat and DatetimeLocale properties in the DatetimeVariableImportOptions.
> In (line 82)
In (line 18)
In (line 122)
In (line 25)
In (line 98)


sriram shastry on 19 Aug 2019 at 13:07
With your help i can move a lot quicker .Thanks a ton .
If can't use fixed width import nor delimiter in readtable , now i am lost.
regexp is much difficult than readtable and i won't prefer since I have to maintain similar coding style for my project if not this is a hard option to do so .Can you please suggest someting else that is easy to do .
on 19 Aug 2019 at 13:30
Can you please suggest someting else that is easy to do
Stick to one file format, the fixed width one in your previous question. Or use an even better one (e.g. comma delimited with text in quoted strings)
Otherwise, I'm afraid there's no easy way to do it. You'll have to use regexp or write your own parser.
sriram shastry on 19 Aug 2019 at 14:05

OK if no other choices left .Can you please help me with sample script here

Sign in to comment.

2 Answers

Answer by sriram shastry on 20 Aug 2019 at 16:19
 Accepted Answer

milliseconds information is in place(.SSSSSS)but conversion is not done properly.


Walter Roberson
on 20 Aug 2019 at 20:03
The conversion looks perfect when I try.
timestamps.Format = 'dd-MMM-uuuu HH:mm:ss.SSSSSS';
>> timestamps(1:2)
ans =
1×2 datetime array
03-Aug-2019 13:58:25.745523 03-Aug-2019 13:58:25.746634
>> S(1:100)
ans =
'[0803/] A/B Update Engine starting
745523 and 746634 exactly match the input text.
sriram shastry on 21 Aug 2019 at 5:58
Thanks a lot for your efforts.

Sign in to comment.

Answer by Walter Roberson
on 19 Aug 2019 at 16:31

S = fileread('update_engine.txt');
timestamps = regexp(S, '^.(.{18})', 'lineanchors', 'match');
ts_dt = datetime(timestamps, 'InputFormat', 'MMdd/HHmmss.SSSSSS');


Show 1 older comment
Walter Roberson
on 20 Aug 2019 at 6:10
S = fileread('update_engine.txt');
pieces = regexp(S, '^\[(?<timestamp>[^:]+):(?<category>[^:]+):(?<filename>[^\(]+)\((?<line>\d+)\)\]\s*(?<details>.*)', 'names','lineanchors','dotexceptnewline');
timestamps = datetime({pieces.timestamp}, 'InputFormat', 'MMdd/HHmmss.SSSSSS');
pieces will be a struct array with fields timestamp (containing the text of the timestamp), category (e.g., INFO), filename (e.g., ''), line (e.g., '182'), and details (e.g., 'A/B Update Engine starting')
sriram shastry on 20 Aug 2019 at 7:51
Thanks a lot Walter.
Why I don't see the milliseconds information "timestamps" ? Any help
Walter Roberson
on 20 Aug 2019 at 16:11
You can change the Format property to display the milliseconds.

Sign in to comment.