RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • fclose
    • feof
    • fgetl
    • fgets
    • fileread
    • filewrite
    • fopen
    • fprintf
    • fread
    • frewind
    • fwrite
    • readlines
    • writelines

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

NameTypeRequiredDefaultDescription
fidNumericScalarYes—File identifier opened by fopen.

Returns

NameTypeDescription
tlineAnyNext line without terminator, or -1 at end-of-file.

Errors

IdentifierWhenMessage
RunMat:fgetl:InvalidInputInput argument count or identifier form is invalid.fgetl: invalid input arguments
RunMat:fgetl:InvalidIdentifierIdentifier does not refer to a readable open file.fgetl: invalid file identifier. Use fopen to generate a valid file ID.
RunMat:fgetl:IoFailureLine 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, \n\r, and \r; the \n\r pair is consumed as one newline sequence.
  • When the file is empty or the file position indicator is already at end-of-file, fgetl returns 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.

Does RunMat run fgetl on the GPU?

fgetl is a host-only operation. Provider-resident identifiers are a gated RunMat extension; when enabled, RunMat gathers the scalar before reading and returns a regular host value.

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);

line

Expected 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
two

Distinguish 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
line3

Expected output:

ans =
  logical
   1
line2 =
    'last'
line3 =
    -1

Read 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
line2

Expected output:

line1 =
    'last line'
line2 =
    -1

Using 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

Filetext

fclose · feof · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines

Net

accept · read · readline · tcpclient · tcpserver · write

Repl Fs

addpath · cd · copyfile · delete · dir · exist · fileattrib · fileparts · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · mkdir · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite

Tabular

arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite

Audio

audioinfo · audioread

Io

clc · diary · disp · display · format · input

Archive

gunzip · gzip · unzip

Hdf5

h5disp · h5info · h5read · h5write · h5writeatt · hdf5info · hdf5read · hdf5write

Import

importdata · textscan

Json

jsondecode · jsonencode

Mat

load · matfile · save

Http

sendmail · urldecode · urlencode · weboptions · webread · websave · webwrite

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How fgetl works
  • Does RunMat run fgetl on the GPU?
  • Examples
  • Read the first line of a file without the newline
  • Read a file line by line
  • Distinguish an empty line from end of file
  • Read a final line without a trailing newline
  • Using fgetl with coding agents
  • FAQ
  • Related Io functions
  • Filetext
  • Net
  • Repl Fs
  • Tabular
  • Audio
  • Io
  • Archive
  • Hdf5
  • Import
  • Json
  • Mat
  • Http
  • Open-source implementation
  • About RunMat