Findpeak from the graph in specific range

14 views (last 30 days)
Harsimranjot Grewal
Harsimranjot Grewal on 26 May 2021
Commented: Allen on 27 May 2021
Hello Everyone,
I am trying to use the findpeaks function in my code but in a specific range.In the code below you can see I have a plot of x2 and y2.From this plot, I want to find the peak at x2 values between 90 and 120.BUt my code doesnt work for the peaks and shows a blank graph.Any help would be appreciated.
Data = xlsread('test.xlsx');
%% Step 2 data
x2 =Data(655:8466,6); % Sample temprature
y2 =Data(655:8466,3); % Umsubstracted temprature
figure
plot(x2,y2);
set(gca,'ydir', 'reverse')
title('Step 2 Data')
hold on
[pks, locs]= findpeaks((x2(90:120)),'Npeaks',1)
title('Peak for step 2')

Answers (1)

Allen
Allen on 26 May 2021
Edited: Allen on 27 May 2021
[pks, locs]= findpeaks((x2(90:120)),'Npeaks',1)
In your above line of code, you are indexing x2 from elements 90 through 120 and not from between the values of 90 and 120. To find the indices of x2 with values in your preferred range use the following instead.
% Between 90 and 120, but not equal to 90 or 120.
idx = x2>90 & x2<120;
% If you want to include values equal to 90 and 120, then use
idx = x2>=90 & x2<=120;
Using the correct index, use the following with findpeaks.
[pks, locs] = findpeaks((x2(idx)),'Npeaks',1);
  2 Comments
Harsimranjot Grewal
Harsimranjot Grewal on 27 May 2021
Thanks for your answer but it didnt work and the command line says ' Array indices must be positive integers or logical values." I just replaced the code with your answer.So, any other recommendations.
Allen
Allen on 27 May 2021
@Harsimranjot Grewal looks like repeated the applying the index to x2 more than once. I have made corrections to my example.
Changed idx = x2(x2>90 & x2<120); to idx = x2>90 & x2<120; and idx = x2(x2>=90 & x2<=120); to idx = x2>=90 & x2<=120; which work better.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!