MATLAB: How to prevent over-writing in the for loop

for loopinterpolationlooploops

My interpolation for loop keeps over-writing the last value, even though I assigned x2=zeros(N,2).
clear; clc;
Folder = cd;
N=100;
x2 = zeros(N, 2);
for k = 1:N;
Driftt = sprintf('Drift%d.out', k);
Reactt = sprintf('React%d.out', k);
matDrift = importdata(fullfile(Folder, Driftt));
matReact = importdata(fullfile(Folder, Reactt));
x1= matDrift(:,2);
y1= -sum(matReact(:,2:11),2);
[x3, ix] = unique(x1);
y3 = y1(ix);
[y2] = [500000, 5000000, 7000000, 8000000, 900000, 13000000, 14000000];
x2 = interp1(y3, x3, y2, 'linear')
end

Best Answer

Your loop needs to provide an index into x2
x2(something)= interp1(y3, x3, y2, 'linear')
to tell it where to store things.