Reading content of a file using readtable return NaT for Time

3 ビュー (過去 30 日間)
Life is Wonderful
Life is Wonderful 2019 年 8 月 20 日
編集済み: Life is Wonderful 2019 年 9 月 5 日
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.
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 9 月 5 日
Maybe
opt = setvartype(opt, 4, 'char');
Andrei Bobrov
Andrei Bobrov 2019 年 9 月 5 日
+1

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by