I am using
fid = fopen('fgfg.txt');
to open a file.
Sometimes an error occurs before I manage to close the file. I can't do anything with that file until I close Matlab.
How can I close a file if an error occurs?
I am using
fid = fopen('fgfg.txt');
to open a file.
Sometimes an error occurs before I manage to close the file. I can't do anything with that file until I close Matlab.
How can I close a file if an error occurs?
First of all, you can use the command
Secondly, you can use try-catch blocks and close your file handles
There is a third approach, which is much better. Matlab is now an object-oriented language with garbage collector. You can define a wrapper object that will take care of its lifecycle automatically.
Since it is possible in Matlab to call object methods both in this way:
and in that way:
You can define a class that mimics all of the relevant file command, and encapsulates the lifecycle.
The delete operator is called automatically by Matlab. (There are more functions that you will need to wrap, (fread, fseek, etc..)).
So now you have safe handles that automatically close the file whether you lost scope of it or an error happened.
Use it like this:
And no need to close.
Edit: I just thought about wrapping
fclose()
to do nothing. It might be useful for backward compatibility - for old functions that use file ids.Edit(2): Following @AndrewJanke good comment, I would like to improve the delete method by throwing errors on fclose()
Andrey's solution above is indeed the best approach to this problem. I just wanted to add that throwing an exception in method
delete()
might be problematic, if you deal with arrays ofsafefopen
objects. During destruction of such an array, MATLAB will calldelete()
on each array element and, if anydelete()
throws, then you might end up with leftover open file handles. If you really need to know whether something went wrong during destruction then issuing a warning would be a better option IMHO.For those that feel lazy to write all the forwarding methods to every MATLAB builtin that uses file handles, you may consider the simple alternative of overloading method
subsref
for classsafefopen
:This alternative uses the somewhat ugly
feval
, but has the advantage of working even if the MATLAB guys (or yourself) decide to add new functions that involve file handles, or if the number/order of the input arguments to a given function change. If you decide to go for thesubsref
alternative then you should use classsafefopen
like this:EDIT: A disadvantage of the
subsref
solution is that it disregards all output arguments. If you need the output arguments then you will have to introduce some more ugliness:And then you could do things like:
%assuming that you want to close all open filehandles
You can try a very neat "function" added by ML called
onCleanup
. Loren Shure had a complete writeup on it when it was added. It's a class that you instantiate with your cleanup code, then it executes when it goes out of scope - i.e. when it errors, or the function ends. Makes the code very clean. This is a generic version of the class that Andrey had above. (BTW, for complex tasks like hitting external data sources, custom classes are definitely the way to go.)from the help:
Basically, you give it a function handle (in this case
@()fclose(fid)
)that it runs when it goes out of scope.Your cleanup code is executed either when an error is thrown OR when it exits normally, because you exit
fileOpenSafely
andc
goes out of scope.No
try/catch
or conditional code necessary.