MATLAB: “Index exceeds matrix dimensions” in imwrite()

Hello everyone, I am trying to save my images using imwrite() but its hitting "Index exceeds matrix dimensions." error at line no 134 i.e at " imwrite(final_img, strcat(output_folder, srcFiles(i).name));" I am not getting how to resolve this error. If any of you have an answer to this please tell me. Thank you!!
if true
clc;
clear all;
srcFiles = dir('C:\Users\Dell\Desktop\Messi_Tiff\abn_1\*.tif');
x= length(srcFiles);
for i=1:x
filename = strcat('C:\Users\Dell\Desktop\Messi_Tiff\abn_1\',srcFiles(i).name);
I = imread(filename);
%imshow(I);
green_cnl = I(:, :, 2);
K = imadjust(green_cnl,[0.13 0.3],[]);
%figure, imshow(K);
img_adap = adapthisteq(K);
%figure, imshow(img_adap);
se = strel('disk',15);
close_img = imclose(img_adap,se);
%figure, imshow(close_img),title('Closeing');
img_sub=imsubtract(close_img,img_adap);
%figure, imshow(img_sub),title('Subtract');
level=graythresh(img_sub);
img_BW = im2bw(img_sub,level);
%figure,imshow(img_BW),title('Binary image using graythresh');
BW_10_test = bwareaopen(img_BW,150);
%figure,imshow(BW_10_test),title('without bessels.................');
[xx,yy]=find(BW_10_test==1);
nn=[xx,yy];
img_BW_test=im2bw(img_sub,0.14);
%figure,imshow(img_BW_test),title('create binary image using 0.15');
img_BW_filter = medfilt2(img_BW_test);
%figure,imshow(img_BW_filter),title('Median Filtering');
BW_10 = bwareaopen(img_BW_filter,10);
%figure,imshow(BW2),title('last line');

BW_100 = bwareaopen(img_BW_filter,100);
%figure,imshow(BW_100),title('last line2');
img1_BW=imsubtract(BW_10,BW_100);
%figure,imshow(img1_BW),title('binary img1 using size 10 to 100');
for i=1:size(nn)
pos_x=nn(i,1);
pos_y=nn(i,2);
img1_BW(pos_x,pos_y)=0;
end
%figure,imshow(img1_BW),title('Binary img1 without vassels pixels');
img_imadjust_sub = img_sub;
%figure, imshow(img_imadjust_sub),title('img_imadjust_sub');
[x1,y1]=find(img_imadjust_sub>=58 & img_imadjust_sub<=155);
n1=[x1,y1];
img2=zeros(size(img_adap));
for i=1:size(n1)
pos_x=n1(i,1);
pos_y=n1(i,2);
img2(pos_x,pos_y)=1;
% test3(pos_x,pos_y)=255;
end
img2_filter=medfilt2(img2);
%figure, imshow(img2_filter),title('im2 using color');
BW2_10 = bwareaopen(img2_filter,10);
%figure,imshow(BW2),title('last line');
BW2_100 = bwareaopen(img2_filter,100);
%figure,imshow(BW2_100),title('last line2');
img2_BW=imsubtract(BW2_10,BW2_100);
%figure,imshow(img2_BW),title('img2 with noise');
for i=1:size(nn)
pos_x=nn(i,1);
pos_y=nn(i,2);
img2_BW(pos_x,pos_y)=0;
end
%figure,imshow(img2_BW),title('img2 with out noise');
final_img=zeros(size(img_adap));
img_add=imadd(img1_BW,img2_BW,'uint8');
[x3,y3]=find(img_add==2);
n3=[x3,y3];
for i=1:size(n3)
pos_x=n3(i,1);
pos_y=n3(i,2);
final_img(pos_x,pos_y)=1;
end
img_mask=imread('D:\mask.tif');
img_mask=im2bw(img_mask);
[xmask,ymask]=find(img_mask==0);
nmask=[xmask,ymask];
for i=1:size(nmask)
x=nmask(i,1);
y=nmask(i,2);
final_img(x,y)=0;
end
%figure,imshow(final_img),title('MA Detection');
%final_img = im2uint8(final_img);
%filename = [sprintf('%03d',i) '.tif'];
output_folder = 'D:\abn_1\';
%fullname = fullfile(output_folder,filename);
%imwrite(IM2,fullname);
imwrite(final_img, strcat(output_folder, srcFiles(i).name));
end
end

Best Answer

I just had a quick look but it seems to me that you're using "i" as an index in different (and nested) loops, so the values in nested loops override your the value of i in the main loop.