The matfile
command opens what seems like a persistent connection to a *.mat
file. In most coding situations where a file is accessed, it is necessary to close said file. There is no mention of that in the page linked to above. It seems unusual to me, but can I assume that no closing is necessary?
The object returned is of the matlab.io.MatFile class, which is a handle class.
In MATLAB, handle classes are a type of class that work differently than normal matrices. They are not copied, they are always passed by reference. Making a copy simply makes a new reference to the object. This type of object is used mostly to own resources. The resources are automatically freed when the last of the references to the object is cleared.
Thus, to close the connection, you can simply delete the variable that holds the reference:
m = matfile(filename);
% ... read/write from file
clear m
...but note that you shouldn't need to do this explicitly, because local variables are automatically cleared at the end of the scope (i.e. when a function returns).
For more information on handle classes, see this page of the docs.