How can I know which file identifiers correspond to open files?
29 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MathWorks Support Team
le 13 Nov 2009
Modifié(e) : Josh Kahn
le 9 Juin 2023
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.
Réponse acceptée
MathWorks Support Team
le 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')
0 commentaires
Plus de réponses (1)
Josh Kahn
le 9 Juin 2023
Modifié(e) : Josh Kahn
le 9 Juin 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
0 commentaires
Voir également
Catégories
En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!