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.
Related Questions
Expert Answer
John Michell
PhD Expert
Answered Aug 25, 2026
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: Youryvalues (the data you want to find peaks for).Scope: Yourxvalues (the corresponding x-axis data for the peaks).
The output will be:
pks: The peak values in theTemperaturearray.locs: The correspondingxvalues from theScopearray 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
- Ensure that
ScopeandTemperaturehave the same length. - Use
findpeakswith bothyandxinputs to get the correctxlocations for the peaks. - 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