for loop skipping values
9 views (last 30 days)
Show older comments
Hi, I have a code for a for loop to find local mins and print them on a graph as a "Q" for a QRS plot. However, when I set a time frame in the middle of the data set and not in the beginning, it gives me all the Q values before the given window as "0"s (in the command window) and does not print out position of "Q" on the graph. Why is it so?
Here's the code for that for loop (also, fs is set to 250 instead of 200, if that makes any difference):
% Loop to find Q values
for i1=xlim_start:1:xlim_end+1; %(10 is an arbitrary number) % limits the search to the interval you set (to make things faster and easier)
var_i = R_pos(1,i1)*fs; % variable to store the position within x4 where an R value appears
%var_i=int8(var_i)
peak_temp=R_vals; % temporary peak value to compare
% The code below takes the peak value, and goes 200 points
% backwards until it reaches a local min value by checking whether
% the value increases or decreases. Once the value starts
% increasing again, the count stops and recognizes a local min
for j1=1:1:30000
if x4(int32(var_i-j1))<=peak_temp
peak_temp=x4(int32(var_i-j1));
elseif x4(int32(var_i-j1))>peak_temp
Q_val(i1,1)=x4(int32(var_i-j1));
Q_pos(i1,1)=(var_i-j1)/fs;
break
end
end
end
0 Comments
Answers (2)
Walter Roberson
on 27 Oct 2016
Your lines
Q_val(i1,1)=x4(int32(var_i-j1));
Q_pos(i1,1)=(var_i-j1)/fs;
start the output into Q_val and Q_pos from whatever the first value of i1 is, so when your loop starts from xlim_start then the first place written to in the Q_val and Q_pos variables would be Q_val(xlim_start) and Q_val(xlim_start) . If you want to start at the beginning of Q_val and Q_pos then you need to use
Q_val(i1 - xlim_start + 1, 1)=x4(int32(var_i-j1));
Q_pos(i1 - xlim_start + 1, 1)=(var_i-j1)/fs;
or you need to rewrite your looping:
xlim_vals = xlim_start : xlim_end + 1; %(10 is an arbitrary number) % limits the search to the interval you set (to make things faster and easier)
% Loop to find Q values
for xlim_idx = 1 : length(xlim_vals)
i1 = xlim_vals(xlim_idx);
var_i = R_pos(1,i1)*fs; % variable to store the position within x4 where an R value appears
%var_i=int8(var_i)
peak_temp=R_vals; % temporary peak value to compare
% The code below takes the peak value, and goes 200 points
% backwards until it reaches a local min value by checking whether
% the value increases or decreases. Once the value starts
% increasing again, the count stops and recognizes a local min
for j1=1:1:30000
if x4(int32(var_i-j1))<=peak_temp
peak_temp=x4(int32(var_i-j1));
elseif x4(int32(var_i-j1))>peak_temp
Q_val(xlim_idx, 1)=x4(int32(var_i-j1));
Q_pos(xlim_idx, 1)=(var_i-j1)/fs;
break
end
end
end
0 Comments
Durna Alakbarova
on 27 Oct 2016
1 Comment
Walter Roberson
on 27 Oct 2016
You need to define for us what your desired output is.
Do you want Q_pos(1:xlim_start-1) to be zero, and Q_pos(xlim_start) to correspond to the result of the first iteration of i1, with Q_pos(xlim_start+1) corresponding to the result of the second iteration of i1?
Do you want Q_pos(1) to correspond to the result of the first iteration of i1, and Q_pos(2) to correspond to the result of the second iteration of i1?
Do you want Q_pos(1) to correspond to the first place at which x4(int32(var_i-j1))>peak_temp was satisfied, which might not be until hundreds of i1 iterations had taken place, and Q_pos(2) should be the next place that it is satisfied, possibly some distance off?
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!