Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

imwrite error while writing tiff stack image

350 views
Skip to first unread message

Jens

unread,
Jan 25, 2011, 10:32:06 AM1/25/11
to
Hi,

I easily want to save an image stack with imwrite:

for i=1:60
imwrite(ImStack(:,:,i),'file.tif','WriteMode','append','Compression','none');
end

This code works fine on another machine with Matlab 2009b (Win XP, x86). On mine with Matlab 2010b 64bit (Win7 64), there occurs an error while saving the image.

??? Error using ==> imwrite at 453
Can't open file "C:/.../stackadd/file.tif" for
writing.
You may not have write permission.

Error in ==> stackadd at 61
imwrite(ImStack(:,:,i),'file.tif','WriteMode','append','Compression','none');

This error does not occur every time. When I restart Matlab the script mostly runs fine (at the beginning of the script, the existance of the output file is checked and the file is deleted where applicable). But during the second run there comes this error. It is not reproducable at one special "i". Sometimes the "i" is lower, sometimes higher.

The final image "file.tif" contains those stacks before the error. Thus, the error can't be an issue with permissions.

Hope you can help me... !

John

unread,
Jan 26, 2011, 8:27:03 AM1/26/11
to
Hi Jens, I believe the issue you are running into is described in this article in Steve Eddins' blog

http://blogs.mathworks.com/steve/2010/11/09/the-mystery-of-the-failing-tiff-append-loop/

"Jens" wrote in message <ihmqdm$9v9$1...@fred.mathworks.com>...

TheDro

unread,
Jul 17, 2015, 9:58:08 AM7/17/15
to
I've come up with a way around Windows Explorer interrupting MATLAB while it's trying to write the file.

imwrite(ImStack(:,:,1),filename) %write the first image
tries = 10; %maximum number of interruptions
kk = 1;
while kk < size(h.vol,3) && tries > 0
kk = kk+1;

try
imwrite(ImStack(:,:,kk),filename,'WriteMode','append')
catch e
if strcmp(e.identifier,'MATLAB:imagesci:imwrite:fileOpen')
pause(0.1) %Let Windows Explorer release the file
kk = kk-1; %Try again
tries = tries-1;
continue
else
rethrow(e)
end
end

end

Hopefully this will be helpful to someone in the future.

Cheers,
Andrew
0 new messages