Borrar filtros
Borrar filtros

How do I go to a previous line when reading a text file?

27 visualizaciones (últimos 30 días)
Below is the section of interest from a text file I'm trying to read.
.
.
.
3 0.68500 35.11850 -180.00000
3 0.68500 30.97850 0.00000
4 0.68500 99.97850 0.00000
4 -0.18500 99.97850 -180.00000
4 -0.18500 105.11850 0.00000
4 0.68500 105.11850 -180.00000
4 0.68500 99.97850 0.00000
5 79.57000 8.96350 0.00000
5 74.42000 8.96350 0.00000
5 74.42000 9.83350 -180.00000
5 79.57000 9.83350 0.00000
5 79.57000 8.96350 -180.00000
.END_BOARD_OUTLINE
.
.
.
The value I'm interested in is in the first column above the .END_BOARD_OUTLINE line, 5, in this case. Initially, I search through the file to find .END_BOARD_OUTLINE. I would then like to read the previous line to get the 5, but I can't find a way to move the cursor (for lack of a better term) up a line so I can read it. Is there a way to do this or do I need a different strategy to find that number?
I'm using fopen to open the file and fgetl to read the lines.

Respuesta aceptada

Andrew Newell
Andrew Newell el 17 de Feb. de 2011
You could store the current and previous lines like this:
currline = fgetl(fid);
while ischar(currline) && ~strcmp(currline,'.END_BOARD_OUTLINE')
prevline = currline;
currline = fgetl(fid);
end
Then prevline contains the line you want.
  2 comentarios
Sam G.
Sam G. el 17 de Feb. de 2011
Yeah, that should work. Thanks! So there's no |goto| commands or anything like that?
Andrew Newell
Andrew Newell el 17 de Feb. de 2011
There is *fseek*, but you have to know how many bytes to go back.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by