MATLAB: How do i plot maximum values

if loop

I am writing a function to plot all the maximum values of a 1D matrix on a graph. When i click 'Run', no figure shows up and the command window just displays all the figures of the 1D matrix. what am i doing wrong?

Best Answer

You weren't actually calling the function. Secondly, the function had no input argument for threshold so I've included that. I also removed the loop as it simplifies the code and makes it more efficient.
ECG = load('ECG.csv');
f_s = 350; %Frequency of ECG [Hz]
Time = (0:length(ECG)-1)/f_s; %Number of samples divided by frquency
Amp = ECG(:,1); %ECG Amplitude
threshold = 100;
figure(), plot(Time,Amp,'b-') % I've pulled this out of the function
[Time_peak, Amp_peak] = findLocalMaxThreshold(Time,Amp,threshold)
hold on; plot(Time_peak, Amp_peak, '*r')
function [xp,yp] = findLocalMaxThreshold(x,y,threshold)
idx = find(y(2:end-1) > (y(1:end-2)) & y(2:end-1) > y(3:end) & y(2:end-1) > threshold ) + 1;
xp = x(idx);
yp = y(idx);
end