How do I import a text file and average data in a column from said file?
1 回表示 (過去 30 日間)
古いコメントを表示
I have a text file full of data which looks like this:
'AHMOD.txt'
01-_04,14,09PM $VNINS,317667.094785,-118.07304581
01-_04,14,09PM $VNINS,317667.294783,-118.07304572
01-_04,14,09PM $VNINS,317667.494783,-118.07304562
01-_04,14,09PM $VNINS,317667.694783,-118.07304613
01-_04,14,09PM $VNINS,317667.894783,-118.07304591
01-_04,14,10PM $VNINS,317668.094783,-118.07304579
01-_04,14,10PM $VNINS,317668.294784,-118.07304591
01-_04,14,10PM $VNINS,317668.494784,-118.07304591
01-_04,14,10PM $VNINS,317668.694784,-118.07304563
01-_04,14,10PM $VNINS,317668.894784,-118.07304520
01-_04,14,11PM $VNINS,317669.094784,-118.07304532
01-_04,14,11PM $VNINS,317669.294785,-118.07304514
01-_04,14,11PM $VNINS,317669.494785,-118.07304524
01-_04,14,11PM $VNINS,317669.694785,-118.07304491
01-_04,14,11PM $VNINS,317669.894785,-118.07304461
01-_04,14,12PM $VNINS,317670.094785,-118.07304486
01-_04,14,12PM $VNINS,317670.294786,-118.07304442
01-_04,14,12PM $VNINS,317670.494786,-118.07304424
01-_04,14,12PM $VNINS,317670.694786,-118.07304395
01-_04,14,12PM $VNINS,317670.894786,-118.07304388
The text file holds 20 instances of where data was collected, 5 instances per second. Each row holds a time stamp (such as: 01-_04,14,09PM) and two collected data values (such as:317667.094785,-118.07304581).
I need to average the numeric values of data that were collected in each second. Can someone help me with the importing and averaging of the data?
Thank you
3 件のコメント
Guillaume
2018 年 8 月 10 日
Your text file doesn't look like what you've shown and is a mess to parse. Can you configure whatever is creating the file to output something more sensible?
採用された回答
Guillaume
2018 年 8 月 10 日
filecontent = fileread('AHMOD.txt');
usefulcontent = regexp(filecontent, '([^ ]+) +\$[^,]+,([^,])+,(.*)$', 'tokens', 'lineanchors', 'dotexceptnewline');
usefulcontent = vertcat(usefulcontent{:});
t = table2timetable(table(...
datetime(usefulcontent(:, 1), 'InputFormat', 'dd-MM-yy_hh,mm,ssa', 'Format', 'default'), ...
str2double(usefulcontent(:, 2)), ...
str2double(usefulcontent(:, 3)), ...
'VariableNames', {'datetime', 'something', 'somethingelse'} ... change to whatever you want
));
tsecond = retime(t, 'secondly', 'mean') %average by second
2 件のコメント
Guillaume
2018 年 8 月 10 日
Sure, instead of
usefulcontent = vertcat(usefulcontent{:});
use
usefulcontent = vertcat(usefulcontent{6:end});
Alternatively, remove the first 6 rows of t:
t(1:6, :) = [];
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!