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

1 vue (au cours des 30 derniers jours)
isamh
isamh le 12 Fév 2020
Réponse apportée : TADA le 12 Fév 2020
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);

Réponse acceptée

TADA
TADA le 12 Fév 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

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by