# How to display 1D vector with indices into a function

1 view (last 30 days)
Ken Chi on 11 Jan 2020
Edited: Ken Chi on 12 Jan 2020
I have created a function that identifies the peaks in an ECG over a particular threshold and plots it onto a graph. I would also want the function to display a 1D vector containing the indicies for each of the local maxima (there are 11) that are above the threshold. I have tried the fprintf way but have had no luck. The vector should display these values:
This is the function that id like to incorportate it into (the threshold = 100):

KALYAN ACHARJYA on 11 Jan 2020
Edited: KALYAN ACHARJYA on 12 Jan 2020
Store those index in some 1 D array and later call the respective index data from ECG data, see the modified code, you may get idea (modification may be required as per desired result)
function [threshold] = LocalMaxThreshold(thres)
threshold = thres;
f_s = 350; %Frequency of ECG [Hz]
n=length(ECG);
t = [0:n-1]/f_s; %Number of samples divided by frquency
idx=[]; l=1;
for k=2:length(ECG)-1
if (ECG(k)>ECG(k-1) && ECG(k) > ECG(k+1) && ECG(k) > threshold) % check the condition carefully
idx(l)=k;
l=l+1;
end
end
plot(t,ECG,'b-',t(idx),ECG(idx),'r*')

Ken Chi on 12 Jan 2020
That worked but gave me the idx as it worked out each in the commant window which cluttered the command window . Is there a way of just displaying all of them at once in the command window in 1-2 rows?
KALYAN ACHARJYA on 12 Jan 2020
function [threshold] = LocalMaxThreshold(thres)
threshold = thres;
f_s = 350; %Frequency of ECG [Hz]
n=length(ECG);
t = [0:n-1]/f_s; %Number of samples divided by frquency
idx=[]; l=1;
for k=2:length(ECG)-1
if (ECG(k)>ECG(k-1) && ECG(k) > ECG(k+1) && ECG(k) > threshold) % check the condition carefully
idx(l)=k;
l=l+1;
end
end
idx
plot(t,ECG,'b-',t(idx),ECG(idx),'r*')