Borrar filtros
Borrar filtros

Get one line from file

32 visualizaciones (últimos 30 días)
bart
bart el 20 de Abr. de 2011
Hey,
I have a problem with extracting information from a textfile. The information I need is only on one line. I know which line it is, but how can I read it from the textfile easily? I've allready considered the command 'fgets', but this only takes the first line of the file, right?.

Respuestas (5)

Walter Roberson
Walter Roberson el 20 de Abr. de 2011
For further certainty: neither fgets() nor fgetl() have any mechanism for positioning to a particular line. Something has to specifically count the lines.
A short-cut is to use
textscan(fid, '%[^\n]', 1, 'HeaderLines', N)
this will do the counting for you.

Jennifer
Jennifer el 20 de Abr. de 2011
Try using something like:
load file.txt
% Create variable
% edit number of rows as needed
var= file(1:10,1);
In the example above we are reading the first 10 rows or column 1.
Hope this helps

Robert Cumming
Robert Cumming el 20 de Abr. de 2011
or you could use
fid = fopen ( 'file.txt' )
if fid ~= -1
for i=1:NUMBER_OF_LINES_TO_SKIP
fgetl ( fid );
end
yourLine = fgetl ( fid );
fclose ( fid );
end

bart
bart el 20 de Abr. de 2011
Thanks for answering the question! I never expected to get an answer so quickly! greets, Bart

bart
bart el 21 de Abr. de 2011
OK, thanks.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by