MATLAB: How to save value of variable in matrix form, which is overwritten after each iteration in for loop

ir calculation of ecg signal

Here is the code that I have written, in this I want all the values of T in each iteration but it overwritten after each iteration and I am getting only last value of T.
load ('100m.mat') % the signal will be loaded to "val" matrix val = (val – 1024)/200; % you have to remove "base" and "gain" ECGsignal = val(1,1:3600); % select the lead (Lead I) Fs = 360; % sampling frequecy t = (0:length(ECGsignal)-1)/Fs; % time subplot(2,1,1) plot(t,ECGsignal) sig = smoothts(ECGsignal, 'b', 5); subplot(2,1,2) plot(t,sig)
%% Beat Rate Calculation beat_count=0; for k = 2 : length(sig)-1 if(sig(k)>sig(k-1) & sig(k)>sig(k+1) & sig(k)>0.5) k disp('Prominant Peak found') beat_count = beat_count+1; R = sig(k); % Value of R peak
T = (k/Fs) % Time at which R peak is occuring
disp('Time at which R peak is occuring(sec)')
end
end
N = length(sig);
Duration_in_seconds = N/Fs;
Duration_in_minutes = Duration_in_seconds/60;
BR = beat_count/Duration_in_minutes

Best Answer

You can save the values in a cell-array. As described in the following answer http://ch.mathworks.com/matlabcentral/answers/259579#answer_202813
Or if it is only a scalar value you can save the values in an array. eg. growing vector (slow)
T_vec = [];
for k=1:5
...
T_vec = [T_vec T];
end
eg. predefined(preallocated) vector (recommended because of performance)
T_vec = zeros(5,1);
for k= 2:6
...
T_vec(k-1) = T;
end