MATLAB: “for” loop question

baseline driftfor loop

Hello,
Below is an excerpt from my code, in which I wish to linearly fix a baseline drift. The variable "QLBrainShift1" is simply the original signal.
AmtOfPts1 = iSO(3) - iSO(2); % Amount of points between SO 2 and 3
error1 = -QLBrainShift1(end); % scalar
for point1 = AmtOfPts1:-1:0
QLBrainShift1a = QLBrainShift1+error1-(point1/AmtOfPts1)*error1;
end
figure
plot(QTime(iSO(2):iSO(3)),QLBrainShift1a)
The plot that is being returned seems like QLBrainShift1a is being overwritten upon each loop. Therefore, only the last loop (point1=0) exits the "for" loop, and gets plotted. My intention is to create a new signal (QLBrainShift1a) according to the equation above with varying "point1" values, and plot all of the new signal on 1 graph. Any help is greatly appreciated.
Thanks, NH

Best Answer

Perhaps as follows? Basically, you've forgotten to index the element of QLBrainShift1a (and maybe also QLBrainShift1) that you're trying to operate on.
point1=AmtOfPts1:-1:0;
for i=1:length(point1)
QLBrainShift1a(i) = QLBrainShift1(i)+error1-(point1(i)/AmtOfPts1)*error1;
end