Reading content of a file using readtable return NaT for Time

3 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de Sept. de 2019
Editada: Andrei Bobrov el 5 de Sept. de 2019
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.

Más respuestas (1)

Andrei Bobrov
Andrei Bobrov el 5 de Sept. de 2019
Editada: Andrei Bobrov el 5 de Sept. de 2019
T = readtable('eventlog.txt','format',...
'%d %{yyyy-MM-dd HH:mm:SS}D %s %s %s','delimiter','|',...
'ReadVariableNames',false);
  1 comentario
Life is Wonderful
Life is Wonderful el 5 de Sept. de 2019
Editada: Life is Wonderful el 5 de Sept. de 2019
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

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by