You can tell MATLAB to close any open files with
fclose('all');
In my experience fclose
applies to all opened files, not just ones opened with fopen
.
Also, since you are running a matlabpool
, each worker (MATLAB.exe process) will need to release their files. To do this, I suggest creating a try
-catch
inside the parfor
like this.
parfor k = 1:length(files1)
try
% your loop content here
catch ME
% cleanup and terminate
fclose('all');
rethrow(ME);
end
end
This way, the worker that bombs out in the loop will run fclose
. Running it in the master MATLAB.exe probably will not be helpful.
However, you can also make sure you close the matlabpool
before you try to delete the files since this should release any file locks that the workers have.