在win7,使用imwrite函数写入多张tiff图像的时候,经常会出现一个错误:
??? Error using ==> writetif at 100
Couldn’t open ‘file.tif’ for writing.
You may not have write permission.
原因是在win7里面,Windows程序管理器(Windows Explorer)在检测到文件改动之后,会lock文件,进行一些操作。此时,会出现权限问题。
解决办法:
(1)换个系统环境(例如win10)
(2)使用下面的程序来实现正确的写入(解决权限问题)
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
参考链接
[1]https://cn.mathworks.com/matlabcentral/newsreader/view_thread/301697
[2]http://blogs.mathworks.com/steve/2010/11/09/the-mystery-of-the-failing-tiff-append-loop/