MATLAB Answers

0

Reading content of a file using readtable return NaT for Time

Asked by sriram shastry on 20 Aug 2019
Latest activity Edited by sriram shastry on 5 Sep 2019
Please find the attached file. I want to use readtable to parse the file using readtable function.
I want Date and message content separatly done.

2 Answers

Answer by Walter Roberson
on 5 Sep 2019
Edited by Andrei Bobrov
on 5 Sep 2019
 Accepted Answer

filename = 'eventlog.txt';
opt = detectImportOptions(filename);
opt = setvartype(opt, 5, 'char');
datatable = readtable(filename, opt);
datatable{:,2} is now the datetime entry, and datatable(:,[3 4 5]) are the fields.
As the fields are delimited, it is not completely clear whether you wanted everything to the end of the line as a single character vector complete with '|' inside, or if you wanted the fields broken out. The above breaks them out.
string(datatable{:,3}) + " | " + string(datatable{:,4}) + " | " + string(datatable{:,5})
would put the fields back together, except with an extra trailing " | " on the lines that had only 4 fields originally.

  2 Comments

Maybe
opt = setvartype(opt, 4, 'char');

Sign in to comment.


Answer by Andrei Bobrov
on 5 Sep 2019
Edited by Andrei Bobrov
on 5 Sep 2019

T = readtable('eventlog.txt','format',...
'%d %{yyyy-MM-dd HH:mm:SS}D %s %s %s','delimiter','|',...
'ReadVariableNames',false);

  1 Comment

Thanks a lot . I will test the implementation.
I have a question if same format file is passed multiple times
say filename1,filename2,filename3, & so on
then I would like to know
  • how can I make a function out it ,if sugested piece of code is called multiple times for different files?
  • how to pass the index to Output of readtable "datatable " ?
"datatable = readtable(filename, opt); "
or
"T = readtable('eventlog.txt','format',...
'%d %{yyyy-MM-dd HH:mm:SS}D %s %s %s','delimiter','|',...
'ReadVariableNames',false);"
T{idx} ?
Content = T{idx}; ???
or
Content = datatable{idx};
how to pass the argument for next routine & what modification is recommended in below code
I would be using below code with the output of readtable
function [joinedtimetable] = get_fieldnames(Content)
[contentfields] = fieldnames(Content);
for fieldidx = 1:numel(contentfields) %iterate over each field of Content
structtable = struct2cell(Content.(contentfields{fieldidx})); %extract the structure within the field by converting it to cell array (avoids having to work out what its name is)
structtable = structtable{1}; %get the table out of the cell
structtable.Date = duration(structtable.Date.Hour, structtable.Date.Minute, structtable.Date.Second); %extract h/m/s and make that a duration.
structtable.Date = structtable.Date - structtable.Date(1); %elapsed time since start of log.
structtable.Properties.VariableNames{1} = 'WeirdDuration'; %rename the time column {system time logs are wired}
structtimetable = table2timetable(structtable(:, {'WeirdDuration','Message'})); %convert to timetable, only keeping Date and Message
structtimetable.Properties.VariableNames{1} = sprintf('Message_%s', contentfields{fieldidx}); %rename Message variable so we know where it came from
structtimetable = rmmissing(structtimetable); %remove invalid rows to avoid problems with synchronize
structtimetable.Properties.RowTimes.Format = 'hh:mm:ss.SSS'; %duration format to support millisecond
if fieldidx == 1
joinedtimetable = structtimetable; %1st time, create output
else
fieldidx
joinedtimetable = synchronize(joinedtimetable, structtimetable, 'union'); %subsequent times, synchronize. Choose whichever method is prefered
end
end

Sign in to comment.