How do I save value of variable in matrix form, which is overwritten after each iteration in for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
goerk
2016년 1월 20일
편집: goerk
2016년 1월 20일
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
댓글 수: 2
Stephen23
2016년 1월 20일
Growing a vector is not recommended as it makes the code slow. You should only use preallocated arrays, and never grow them in a loop:
Beginners typically grow their arrays in loops, then come here and complain that their code is slow. The solution is simple: preallocate arrays and do not grow them!
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!