Reading content of a file using readtable return NaT for Time

3 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2019-9-5
编辑:Andrei Bobrov 2019-9-5
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.

更多回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by