How can I close files that are left open after an error?

First of all, you can use the command

fclose all

Secondly, you can use try-catch blocks and close your file handles

 try
     f = fopen('myfile.txt','r')
     % do something
     fclose(f);
 catch me
     fclose(f);
     rethrow(me);
 end

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:

myObj.method()

and in that way:

method(myObj)

You can define a class that mimics all of the relevant file command, and encapsulates the lifecycle.

classdef safefopen < handle
    properties(Access=private)
        fid;
    end

    methods(Access=public)
        function this = safefopen(fileName,varargin)            
            this.fid = fopen(fileName,varargin{:});
        end

        function fwrite(this,varargin)
            fwrite(this.fid,varargin{:});
        end

        function fprintf(this,varargin)
            fprintf(this.fid,varargin{:});
        end

        function delete(this)
            fclose(this.fid);
        end
    end

end

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:

f = safefopen('myFile.txt','wt')
fprintf(f,'Hello world!');

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()

    function delete(this)          
        [msg,errorId] = fclose(this.fid);
        if errorId~=0
            throw(MException('safefopen:ErrorInIO',msg));
        end
    end

Leave a Comment