Why do I receive a 'Couldn't open file for writing' error when using IMWRITE on MATLAB 7.8 (R2009a)?

44 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
for i=1:size(I,3)
imwrite(uint16(I(:,:,i)),tname,'WriteMode','append','Compression','none');
end
end
It will sporatically produce the following error:
??? Error using ==> writetif at 100
Couldn't open 'file.tif' for writing.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 19 Jul 2013
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.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2009a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!