MATLAB: I get extra columns with irrelevant results

columnsexcelget distanceimage processingimdistlinepixelsrows

Hi
I use those lines to get the number of pixels among several rows and columns
for col = 390 : 466
h = imdistline(gca,[427 427],[215 370]);
dist(col) = getDistance(h)
end
Then I use this line to export them in excel
data = [dist(:)];
t = table(data);
writetable(t,'YourFile555.xls','Sheet',1,'Range','AD1:AD520')
Other times, when the Range exceeds the pixel values of the image I am supposed to get from dist, then the exceeding cells of the excel would get 0 value.
In my case, I get the correct values up until the 466 column as I have defined in the for loop and then it fills with irrelevant values, the rest of the columns until 520 as I have declared in the Range. Why is that?
155 is the correct value of the image I am using, the rest 83 that you see are values of a previous image. Not the one I am currently use. Why Matlab gets confused??
Columns 457 through 475
155 155 155 155 155 155 155 155 155 155 83 83 83 83 83 83 83 83 83
Columns 476 through 494
83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83
Columns 495 through 508
83 83 83 83 83 83 83 83 83 83 83 83 83 83
I don;t have any other line in the code that refers to the previous image. Just the ones I write here.
Any possible explanation please?

Best Answer

My guess is that you are writing into the same file as the previous image, but with a shorter list of values. The preceding values are still there because you did not clear them and the list of new values is too short to overwrite them all.