Matlab on Windows holds files open

Windows users are more likely to have issues with a locked file in one program that was used in another program. This happens in many programs including Python, Matlab and GNU Octave since Windows locks the open file by default.

Matlab symptoms arise from scenarios including:

  • HDF5 file that was previously accessed in Matlab
  • .m files and folders that were previously edited in Matlab
  • other text files opened in Matlab Editor

Fix

The first think to try in Matlab or GNU Octave is:

fclose('all')

which closes all open files.

If this fails, there could be a bug in Matlab or your code that isn’t closing the file. This can happen when error catching doesn’t have an fclose() inside the catch stanza.

Workaround

The last resort workaround is:

  1. completely close all Matlab sessions
  2. move or delete the files that Matlab previously accessed
  3. reopen Matlab