RunMat
GitHub

fclose — Close file identifiers in MATLAB and RunMat.

fclose closes files previously opened with fopen. Pass a file identifier, a vector of identifiers, or 'all' (or no arguments) to close handles. Status and optional message outputs, including invalid-identifier behavior, follow MATLAB semantics.

Syntax

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

Inputs

NameTypeRequiredDefaultDescription
fidAnyYesFile 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([fid1 fid2 ...]) closes a vector of identifiers. If any identifier is invalid, status is -1 and message explains the failure.
  • status = fclose('all') (or fclose() with no arguments) closes every open file except the standard streams (0, 1, 2).
  • [status, message] = fclose(...) returns the diagnostic message. On success, message is an empty character vector.
  • 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. If the argument resides on a GPU array (for example, 'all' stored in a gpuArray), RunMat gathers the value to host memory before dispatching 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, message] = fclose(9999);
if status == -1
    fprintf('Close failed: %s\n', message);
end

Expected output:

status = -1;
message = 'Invalid file identifier. Use fopen to generate a valid file ID.'

Close multiple file identifiers together

fids = fopen('all');
status = fclose(fids)

Expected output:

status = 0

Detect failures with the second output

[fid, msg] = fopen('data.bin', 'r');
assert(fid ~= -1, msg);
fclose(fid);
[status, message] = fclose(fid);  % closes again, returns -1 and an error string

Expected output:

status = -1;
message = 'Invalid file identifier. Use fopen to generate a valid file ID.'

Close files using the no-argument form

% Equivalent to fclose('all')
status = fclose()

Expected output:

status = 0

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?

Pass a numeric file identifier (scalar or array) returned by fopen, or the keyword 'all'. Calling fclose() with no arguments is equivalent to fclose('all').

What does the status code mean?

0 indicates that every requested identifier was successfully processed. -1 means that at least one identifier was invalid; the optional second output contains the diagnostic message.

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.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fclose is executed, line by line, in Rust.

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.