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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
fid | Any | Yes | — | File identifier scalar, vector, cell list, or keyword "all". |
mode | StringScalar | Yes | "all" | Close all open user file identifiers. |
Returns
| Name | Type | Description |
|---|---|---|
status | NumericScalar | 0 on success, -1 on failure. |
msg | StringScalar | Failure message when status is -1. |
Returned values from fclose depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:fclose:InvalidInput | Input argument count or file identifier value/type is invalid. | fclose: invalid input arguments |
RunMat:fclose:InvalidIdentifier | The provided file identifier does not refer to an open file. | fclose: invalid file identifier. Use fopen to generate a valid file ID. |
RunMat:fclose:IoFailure | Closing 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 identifierfid. The status is0on success and-1if the identifier is invalid.status = fclose([fid1 fid2 ...])closes a vector of identifiers. If any identifier is invalid, status is-1andmessageexplains the failure.status = fclose('all')(orfclose()with no arguments) closes every open file except the standard streams (0, 1, 2).[status, message] = fclose(...)returns the diagnostic message. On success,messageis an empty character vector.- Identifiers 0, 1, and 2 refer to standard input, output, and error.
fclosetreats 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 = 0Close all open files at once
% Close every file except stdin/stdout/stderr
status = fclose('all')Expected output:
status = 0Handle invalid file identifiers gracefully
[status, message] = fclose(9999);
if status == -1
fprintf('Close failed: %s\n', message);
endExpected 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 = 0Detect 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 stringExpected 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 = 0Using 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.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempdir · tempname · uigetfile · uiputfile
Tabular
csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · readmatrix · spreadsheetImportOptions · writecell · writematrix · xlsread
Import
Json
Archive
Http
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.