Borrar filtros
Borrar filtros

How to delete repeating header lines from text file

2 visualizaciones (últimos 30 días)
Bruno Rodriguez
Bruno Rodriguez el 15 de Sept. de 2016
Comentada: Star Strider el 16 de Sept. de 2016
I have a text file consisting of a recurring sequence, made up of 4 header lines followed by 37 lines of data, then another 4 header lines, and so on (see attached txt file). One complication is that each set of headers is slightly different since it incorporates varying numbers. I want to delete all of these header lines and be left with the remaining data.

Respuesta aceptada

Star Strider
Star Strider el 15 de Sept. de 2016
There is no end-of-file in the file you posted. Normally, I would use this code:
fidi = fopen('Bruno Rodriguez a160628.dat','r');
k1 = 1;
while ~feof(fidi)
data(k1) = textscan(fidi, repmat('%f',1,20), 'HeaderLines',4, 'CollectOutput',1, 'EndOfLine','\r\n');
fseek(fidi,0,0);
k1 = k1 + 1;
end
With the file you posted, it is necessary to detect the last record differently. I used fgetl and strfind, copying part of the last header manually to the strfind call.
This works:
fidi = fopen('Bruno Rodriguez a160628.dat','r');
lastrecord = 0; % Initialise ‘lastrecord’
k1 = 1; % Initialise Counter
while ~feof(fidi)
data(k1) = textscan(fidi, repmat('%f',1,20), 'HeaderLines',4, 'CollectOutput',1, 'EndOfLine','\r\n');
if lastrecord % If Previous Was ‘lastrecord’, Stop
break
end
fseek(fidi,0,0); % Restart At New Position
firstline = fgetl(fidi); % Read First Line
lastrecord = strfind(firstline, 'SJSU_Sodar_DiabloCanyon 06/28/2016 23:50:00'); % Find Last Record Section
fseek(fidi, -1, 0); % Back Up One Line So ‘textscan’ Will Read It
k1 = k1 + 1; % Increment Counter
end
  2 comentarios
Bruno Rodriguez
Bruno Rodriguez el 16 de Sept. de 2016
Yep, this works to perfection. Thank you!
Star Strider
Star Strider el 16 de Sept. de 2016
My pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 15 de Sept. de 2016
Is the "SJSU" always going to be the same? If so then you could perhaps take advantage of the CommentStyle of textscan()
  1 comentario
Bruno Rodriguez
Bruno Rodriguez el 15 de Sept. de 2016
Yes, the SJSU will always be the same. Is there a way to consistently delete the 3 lines after that too? I don't think I can make the assumption that the following 3 lines will start the same way each time.

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by