Search code examples
matlabparfor

Killing a script in the middle of a parfor: how to close open files?


When I interrupt a script in the middle of a parfor, some files are still opened by Matlab (e.g. I cannot delete them). I usually call flcose('all') to close all open files but it doesn't seem work in this case. What should I do?


Solution

  • That's because the parfor creates a bunch of Matlab slaves who have their own file handles. The two main solutions are:

    parfor i=1:12 % 12 or some number over your number of workers
       fclose('all')
    end
    

    or simply closing the parallel pool either using matlabpool close or the GUI (bottom left):

    enter image description here

    Note that you can use Process Explorer to find what process is holding a file open in Windows:

    enter image description here