You grow numbers by one row for each subblock. Your line 68 extending numbers would fail of the number of digits in length(B) changed. You are constructing a char column vector that is mostly '0' characters, it appears.
Then each subblock, you write out all of the accumulate numbers to the rectangular area A1:AN40 even though it is only one column.
Ah... looking more closely that is not quite right. You initialize
and later
numbers = [number ; num2str(length(B))];
This does not grow the array. You initialized number (no trailing 's') to [] so numbers (with trailing 's') will always become just num2str(length(B)) which will be a single character vector. And that is what you write out over all of A1:AN40 each time.
To accomodate different lengths you should be using a cell array of character vectors. For efficiency you should pre-allocate it to have the same number of elements as ca has. And you should leave the xlswrite() until after the nested for loops.
You should also pay more attention to all of those imshow() you have. They are erasing one another inside the same subplot, which is an expensive operation (image() instead of imshow() would do less in that regards.) There is no point in plotting those things only to erase them and immediately plot over them, since they will never be visible. Just plot the final image and the count.