fgetl — Read the next line without newline characters in MATLAB and RunMat.
fgetl(fid) reads the next line from a text file and removes trailing line terminators from the returned character row vector. End-of-file signaling and file-position behavior follow MATLAB semantics, so empty lines remain distinguishable from EOF.
Syntax
tline = fgetl(fid)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
fid | NumericScalar | Yes | — | File identifier opened by fopen. |
Returns
| Name | Type | Description |
|---|---|---|
tline | Any | Next line without terminator, or -1 at end-of-file. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:fgetl:InvalidInput | Input argument count or identifier form is invalid. | fgetl: invalid input arguments |
RunMat:fgetl:InvalidIdentifier | Identifier does not refer to a readable open file. | fgetl: invalid file identifier. Use fopen to generate a valid file ID. |
RunMat:fgetl:IoFailure | Line read or decode fails. | fgetl: file I/O failed |
| — | Internal control-flow conversion failed. | fgetl: internal error |
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.
Does RunMat run 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 =
-1Using fgetl with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how fgetl changes the result.
Run a small fgetl example, explain the result, then change one input and compare the output.
FAQ
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 is executed, line by line, in Rust.
- View the source for fgetl in Rust on GitHub
- Learn how the RunMat 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 blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.