How to find the peaks (both x and y location)

M
Muhammad · Feb 23, 2021 · 2K views
Question
For instance, if I was  plotting Scope (x) and Temperature (y), why can't I use the following code: [pks,locs] = findpeaks(Temperature)? Matlab tells me that there is the following error: Error using findpeaks>parse_inputs (line 131) Expected a string for the parameter name, instead the input type was 'double'.   Error in findpeaks (line 71) [X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(Xin,varargin{:});   It gives me the peaks when I simply use findpeaks(Temperature), but I need the corresponding x values to compute as well.
Expert Answer
Profile picture of John Michell
John Michell PhD Expert
Answered Nov 20, 2025

The error occurs because findpeaks expects additional inputs (parameters) to be specified as name-value pairs, and you might have accidentally passed a numeric input that caused confusion.

If you want both the peak values (y locations) and their corresponding x locations, you need to pass the x values explicitly as the first argument to findpeaks. Here's the correct way to do it:

Correct Code

 

[pks, locs] = findpeaks(Temperature, Scope);

Explanation

  • Temperature: Your y values (the data you want to find peaks for).
  • Scope: Your x values (the corresponding x-axis data for the peaks).

The output will be:

  • pks: The peak values in the Temperature array.
  • locs: The corresponding x values from the Scope array where the peaks occur.

Example

 

Scope = 1:0.1:10; % Example x-axis values
Temperature = sin(Scope) + 0.1*rand(size(Scope)); % Example y-axis values with noise

[pks, locs] = findpeaks(Temperature, Scope);

% Plot results
plot(Scope, Temperature);
hold on;
plot(locs, pks, 'ro'); % Mark peaks
xlabel('Scope');
ylabel('Temperature');
title('Peaks in Temperature Data');

Key Points

  1. Ensure that Scope and Temperature have the same length.
  2. Use findpeaks with both y and x inputs to get the correct x locations for the peaks.
  3. If you need additional options (e.g., threshold, minimum peak distance), you can specify them as name-value pairs:

 

[pks, locs] = findpeaks(Temperature, Scope, 'MinPeakHeight', 0.5, 'MinPeakDistance', 10);

Let me know if you need further clarification!

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!