fclose — Close one file, multiple files, or all files opened with fopen.
fclose closes files that were previously opened with fopen. Pass a file identifier, an array of identifiers, or the keyword 'all' to close the desired handles. The first output (status) is 0 when the close succeeds and -1 otherwise. The optional second output (message) contains diagnostic text when an identifier is invalid.
How fclose works in RunMat
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.
How fclose runs 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 = 0FAQ
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 functions to explore
These functions work well alongside fclose. Each page has runnable examples you can try in the browser.
fopen, fread, fwrite, fileread, filewrite, feof, fgets, fprintf, frewind
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fclose works, line by line, in Rust.
- View fclose.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.