Why do I receive a 'Couldn't open file for writing' error when using IMWRITE on MATLAB 7.8 (R2009a)?
59 views (last 30 days)
I have a section of code that uses IMWRITE to append to a TIFF file while inside of a loop:
I = rand([400 400 61],'double')*1500;
tname = 'file.tif';
for j = 1:52
It will sporatically produce the following error:
??? Error using ==> writetif at 100
Couldn't open 'file.tif' for writing.
This error occurs when the Windows Explorer is open to the directory containing the file that is being rewritten. The Explorer will sense that the file has changed and will update its available information, momentarily blocking the file from being rewritten. If IMWRITE is called during this time it will produce this error.
One workaround is to ensure that no Explorer windows are open to the location of the file being written. Another workaround is to write to a temp directory or a file with a different extension (not .tif). This could prevent the lock up issue. Another statement at the end of the loop could rename or move this temp file to the actual tif the user wanted to write.