How can I know which file identifiers correspond to open files?
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 13 de Nov. de 2009
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.
Respuesta aceptada
MathWorks Support Team
el 13 de Nov. de 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 comentarios
Más respuestas (1)
Josh Kahn
el 9 de Jun. de 2023
Editada: Josh Kahn
el 9 de Jun. de 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 comentarios
Ver también
Categorías
Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!