MATLAB: How to sample data at set intervals and then plot

intervalmaxplot

I am currently trying to create code that samples ECG data at certain intervals and extracts the maximum value between each interval. I believe my code does this as the ouput A returns the peaks of the data. However when i try and then plot these peaks it instead plots it as the sample number. E.g. one of my peaks is 257 in amplitude but the graph plots it as the 257th sample instead of the sample corresponding to the max value. I have attched an example of what my code plots. Any help is greatly appreciated!
ECG = load('ECG.csv');
Fs = 350;
frame_duration = 0.5;
frame_len = frame_duration*Fs;
N = length(ECG);
num_frames = floor(N/frame_len);
Time = (0:length(ECG)-1)/Fs; %Number of samples divided by frquency
Amp = ECG(:,1); %ECG Amplitude
hold on
for k = 1:num_frames
frame = ECG((k-1)*frame_len + 1: frame_len*k);
max_val = max(frame);
if (max_val > 100)
A = max_val
figure
plot(Time,Amp,Time(A),Amp(A),'r*')
end
hold off
end

Best Answer

You're giving the max value as the index when what you should be getting from max() is the index. And you don't need A at all.
Try
[max_val, indexAtMax] = max(frame);
if (max_val > 100)
hFig = figure
plot(Time,Amp, Time(indexAtMax),Amp(indexAtMax),'r*')
hFig.WindowState = 'maximized'; % Maximize the window, if desired.