Text file has headers that are 2X4 and are repeated randomly within the data.

1 次查看(过去 30 天)
txt file is:
R E V D
ms km/h km/h -
689427 0.0 0.00 0.0000
689527 0.0 0.00 0.0000
689627 0.0 0.00 0.0000
689727 0.0 0.00 0.0000
689827 0.0 0.00 0.0000
689927 0.0 0.00 0.0000
690027 0.0 0.00 0.0000
690127 0.0 0.00 0.0000
690227 0.0 0.00 0.0000
headers are repeated randomly across the numerical data. I want to ignore the headers when ever they show up. how would I do that?
the TXT file is about 100000 rows down and 4 columns.
my code is:
result = [];
tic
fid=fopen('MCT_Data.txt');
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
celldata = textscan(tline,'%f %f %f %f %f %f');
matdata = cell2mat(celldata);
% match fails for text lines, textscan returns empty cells
result = [result ; matdata];
end
toc
fclose(fid);

采纳的回答

TADA
TADA 2020-2-12
str = fileread('MCT_Data.txt');
nums = cellfun(@str2double, regexp(str, '([\d.,]+)', 'match'));
x = reshape(nums, 4, [])'
x =
689427 0 0 0
689527 0 0 0
689627 0 0 0
689727 0 0 0
689827 0 0 0
689927 0 0 0
690027 0 0 0
690127 0 0 0
690227 0 0 0

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by