fgetl — Read the next line from a file, removing newline characters from the result.
fgetl(fid) returns the next line from the text file associated with fid, removing any line terminator from the returned character row vector. It mirrors MATLAB's end-of-file sentinel and file-position semantics so line-oriented scripts can distinguish real empty lines from EOF.
Syntax
line = fgetl(fileID)fileIDis a file handle returned byfopen. The file must be opened with read permission ('r','r+','w+', or'a+').lineis a character row vector without the trailing line terminator. When the file position is already at end-of-file,fgetlreturns the numeric sentinel-1.
How fgetl works
tline = fgetl(fid)reads from the current file position to the next line terminator or to end-of-file when no terminator is found.- Line terminators are consumed but not included in the returned character row vector. RunMat recognises
\n,\r\n, and\r. - When the file is empty or the file position indicator is already at end-of-file,
fgetlreturns the numeric sentinel-1. - An empty line returns an empty character row vector, not
-1. - Lines are decoded using the text encoding recorded by
fopen. UTF-8, US-ASCII, ISO-8859-1 (latin1), Windows-1252, Shift_JIS, and binary mode are recognised without additional user work.
How RunMat runs fgetl on the GPU
fgetl is a host-only operation. File identifiers live in the host registry created by fopen, so GPU-resident scalar arguments are gathered back to the CPU before the read occurs.
Examples
Read the first line of a file without the newline
% Browser sandbox: you must be logged in
% to save files for this example.
fname = tempname;
fid = fopen(fname, 'w');
fprintf(fid, 'RunMat\nSecond line\n');
fclose(fid);
fid = fopen(fname, 'r');
line = fgetl(fid);
fclose(fid);
delete(fname);
lineExpected output:
line =
'RunMat'Read a file line by line
% Browser sandbox: you must be logged in
% to save files for this example.
fname = tempname;
fid = fopen(fname, 'w');
fprintf(fid, 'one\n');
fprintf(fid, 'two\n');
fclose(fid);
fid = fopen(fname, 'r');
tline = fgetl(fid);
while tline ~= -1
disp(tline);
tline = fgetl(fid);
end
fclose(fid);
delete(fname)Expected output:
one
twoDistinguish an empty line from end of file
% Browser sandbox: you must be logged in
% to save files for this example.
fname = tempname;
fid = fopen(fname, 'w');
fprintf(fid, '\nlast');
fclose(fid);
fid = fopen(fname, 'r');
line1 = fgetl(fid);
line2 = fgetl(fid);
line3 = fgetl(fid);
fclose(fid);
delete(fname);
isempty(line1)
line2
line3Expected output:
ans =
logical
1
line2 =
'last'
line3 =
-1Read a final line without a trailing newline
% Browser sandbox: you must be logged in
% to save files for this example.
fname = tempname;
fid = fopen(fname, 'w');
fprintf(fid, 'last line');
fclose(fid);
fid = fopen(fname, 'r');
line1 = fgetl(fid);
line2 = fgetl(fid);
fclose(fid);
delete(fname);
line1
line2Expected output:
line1 =
'last line'
line2 =
-1FAQ
What does fgetl return at end-of-file?⌄
When no characters can be read because the file position indicator is at end-of-file, fgetl returns the numeric value -1.
How does fgetl differ from fgets?⌄
fgetl removes newline characters from the returned line, while fgets keeps them. Use fgetl for newline-free line processing and fgets when terminator bytes matter.
What happens for a blank line?⌄
A blank line returns an empty character row vector. Only EOF before any characters are read returns -1.
Can I call fgetl on files opened for writing only?⌄
No. The file must be opened with read permission (for example 'r', 'r+', or 'w+'). Calling fgetl on a write-only identifier raises an error.
Related Io functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fgetl works, line by line, in Rust.
- View fgetl.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.