3

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?

user3658425
  • 139
  • 7
  • 2
    Since the interrupt signal is an exception (I think), you could catch it with a `try...except` block and use the onCleanup object: http://stackoverflow.com/questions/1098149/how-do-you-handle-resources-in-matlab-in-an-exception-safe-manner-like-try – darthbith Jun 15 '14 at 17:12

1 Answers1

2

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

Franck Dernoncourt
  • 69,497
  • 68
  • 312
  • 474