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!