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

1 Ansicht (letzte 30 Tage)
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);

Akzeptierte Antwort

TADA
TADA am 12 Feb. 2020
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

Weitere Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by