Reading content of a file using readtable return NaT for Time

조회 수: 2 (최근 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일
편집: 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에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by