Main Content


Close one or all open files



fclose(fileID) closes an open file.

fclose('all') closes all open files.

status = fclose(___) returns a status of 0 when the close operation is successful. Otherwise, it returns -1. You can use this syntax with any of the input arguments of the previous syntaxes.


collapse all

Open the badpoem.txt file, read the first line, and then close the file.

Use fopen to open the file. This function assigns a unique file id to use for reading and writing to the file.

fid = fopen('badpoem.txt')
fid = 3

Read the first line from the file using fgetl.

ans = 
'Oranges and lemons,'

Close the file.


Input Arguments

collapse all

File identifier of an open file, specified as an integer. Before closing a file with fclose, you must use fopen to open the file and obtain its fileID.

Data Types: double

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a