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

fclose — Close file identifiers in MATLAB and RunMat.

fclose closes files previously opened with fopen. MATLAB-compatible mode accepts one scalar double file identifier or the character vector 'all' and returns at most one double status. RunMat's collection, typed-integer, logical, string, resident, no-input, and diagnostic-message forms are separately gated extensions.

Syntax

status = fclose()
status = fclose(fid)
status = fclose("all")
[status, msg] = fclose(...)

Inputs

NameTypeRequiredDefaultDescription
fidAnyYes—File identifier scalar, vector, cell list, or keyword "all".
modeStringScalarYes"all"Close all open user file identifiers.

Returns

NameTypeDescription
statusNumericScalar0 on success, -1 on failure.
msgStringScalarFailure message when status is -1.

Returned values from fclose depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:fclose:InvalidInputInput argument count or file identifier value/type is invalid.fclose: invalid input arguments
RunMat:fclose:InvalidIdentifierThe provided file identifier does not refer to an open file.fclose: invalid file identifier. Use fopen to generate a valid file ID.
RunMat:fclose:IoFailureClosing an open file failed due to I/O error.fclose: failed to close file
—Internal runtime control-flow or conversion failed.fclose: internal error

How fclose works

  • status = fclose(fid) closes the file represented by the numeric identifier fid. The status is 0 on success and -1 if the identifier is invalid.
  • status = fclose('all') closes every open file except the standard streams (0, 1, 2). The documented keyword is a character vector.
  • The file identifier must be a finite, integer-valued scalar double in MATLAB-compatible mode. RunMat validates the complete request before performing a close.
  • Typed integer identifiers, identifier collections, logical identifiers, string-scalar 'all', resident identifiers, fclose() with no input, and [status, message] are available only when their RunMat extensions are enabled.
  • Identifiers 0, 1, and 2 refer to standard input, output, and error. fclose treats them as already-open handles and simply returns success.
  • RunMat keeps file metadata in a registry shared with fopen, ensuring MATLAB-compatible behaviour across subsequent I/O builtins.

Does RunMat run fclose on the GPU?

fclose does not perform GPU computation. A resident numeric identifier is rejected before provider lookup in MATLAB-compatible mode. With the resident-fileID extension enabled, RunMat gathers it through its owning provider and then dispatches the host-only close logic.

Examples

Close a file after writing data

[fid, msg] = fopen('results.txt', 'w');
if fid == -1
    error('Failed to open file: %s', msg);
end
fprintf(fid, 'Simulation complete\n');
status = fclose(fid)

Expected output:

status = 0

Close all open files at once

% Close every file except stdin/stdout/stderr
status = fclose('all')

Expected output:

status = 0

Handle invalid file identifiers gracefully

status = fclose(9999);
if status == -1
    fprintf('Close failed\n');
end

Expected output:

status = -1

Using fclose with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how fclose changes the result.

Run a small fclose example, explain the result, then change one input and compare the output.

FAQ

What values can I pass to fclose?⌄

For MATLAB-compatible code, pass one scalar double identifier returned by fopen, or the character vector 'all'. Other accepted RunMat forms are explicit extensions.

What does the status code mean?⌄

0 indicates success and -1 indicates failure. MATLAB-compatible mode exposes that single status output; RunMat's diagnostic second output is extension-gated.

Does fclose close standard input/output?⌄

Identifiers 0, 1, and 2 always refer to the process standard streams. fclose accepts them but leaves the streams open, returning a success status.

Can I call fclose multiple times on the same identifier?⌄

Yes. The first call closes the file and subsequent calls return status -1 with the message "Invalid file identifier. Use fopen to generate a valid file ID."

Does fclose flush buffered writes?⌄

Closing a file flushes buffered writes and releases the underlying operating system descriptor, matching MATLAB behaviour.

Do I need to close files explicitly when using GPU arrays?⌄

Yes. GPU residency does not change the lifecycle of file handles. Use fclose to release identifiers created with fopen regardless of where the arguments reside.

Can fclose close files opened by other processes?⌄

No. It only closes identifiers that the current RunMat process opened via fopen.

Related Io functions

Filetext

feof · fgetl · 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 fclose is executed, line by line, in Rust.

  • View the source for fclose 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 fclose works
  • Does RunMat run fclose on the GPU?
  • Examples
  • Close a file after writing data
  • Close all open files at once
  • Handle invalid file identifiers gracefully
  • Using fclose 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