MATLAB Answers


Using fgetl() to read lines

Asked by ZK
on 10 Jan 2013

How can I choose different line to read instead first line, using fgetl() formula?

Thank you.




No products are associated with this question.

2 Answers

Answer by Amith Kamath
on 10 Jan 2013
 Accepted answer may be of some help. Unless you are constrained to using fgetl() for some reason,

C = textscan(fid, '%s','delimiter', '\n');
% Setup the line number in LineNum,

should do the trick.


on 11 Jan 2013

Thak You, I used this function, and it's working freat. It's very usefull that it's save as char.

I have one more question, can I choose two lines to read like second and third in one time, how can I do it?

I agree with Walter that this stores the entire file into the cell array, and isn't technically skipping lines. You could possibly choose more lines by using strcat on C{1}{LineNum1} and C{1}{LineNum2}.

on 11 Jan 2013

Thank You I will remember that.

Answer by Walter Roberson
on 10 Jan 2013

There is no method at the operating system level to position by line. The operating system itself uses fgetl() (or fgets()) to move forward by lines. MATLAB does not provide any "wrapper" over fgetl() to support skipping lines.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!