Filter löschen
Filter löschen

How can I know which file identifiers correspond to open files?

72 Ansichten (letzte 30 Tage)
I have opened a file using FOPEN and processed it. A few lines of code later, there was an error, and the file wasn't closed properly.
I would like to know which file identifiers correspond to open files so I can close them using FCLOSE.

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 13 Nov. 2009
To see the list of identifiers of all open files you can use the following command:
fids = fopen('all')
To see the file names which correspond to those file identifiers you can use the following command:
filenames = arrayfun(@fopen, fids, 'UniformOutput', 0)
Finally, if you just want to close all open files you can use the following command:
fclose('all')

Weitere Antworten (1)

Josh Kahn
Josh Kahn am 9 Jun. 2023
Bearbeitet: Josh Kahn am 9 Jun. 2023
Also, if you want to avoid this, you can now use a cleanup object to close the file when the function is done (error or normal) similar to a try/catch.
For more information, see:
function myFunction
fid = fopen('myFile.txt', 'w')
cleanup = onCleanup(@() fclose(fid));
fprintf(fid, 'hello!');
end
equivalent to:
function myFunction
fid = fopen('myFile.txt', 'w')
try
fprintf(fid, 'hello!');
fclose(fid);
catch ME
fclose(fid);
rethrow(ME);
end
end

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by