You are using the find function and then assigning values based on that to the variable Xidx2{kidx}. If you use the debugger with the change you mentioned you will notice that on the first run there is exactly one return value for the line:
index2 = find(abs(f2-k)<10^(-10));
Therefore the value being passed in the following assignment is a scalar:
Xidx2{kidx} = x(index2);
Therefore you get an error in the indexing that follows:
D(kidx) = Xidx2{kidx}(2);
There is nothing in this code which will guarantee that there are multiple values which satisfy the inequality:
abs(f2-k)<10^(-10)
You could use an if statement or try-catch block to perform this check for you.