Error in findpeaks when trying to find the x corodinate

Hello,
I have been testing out the "findpeaks" function and have found that when I use:
[pks,locs] = findpeaks(a),
The Y value of the peaks is correct, but the locs value is just the indices.
Then I use
[pks,locs] = findpeaks(a,t)
I get 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{:});
Error in Damping (line 36)
[pks,locs] = findpeaks(a,t);
I have looked online for some solutions and have found that I get the same error when using this script from this thread: http://www.mathworks.com/matlabcentral/answers/158309-how-to-find-the-peaks-both-x-and-y-location
% Create the sample data
x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [4 4 4 2 2 3];
Wdt = [2 6 3 3 4 6]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss);
plot(x,Gauss,'--',x,PeakSig)
% Use findpeaks with default settings to find the peaks of the signal and their locations.
[pks,locs] = findpeaks(PeakSig,x)
% Plot the peaks using findpeaks and label them.
findpeaks(PeakSig,x)
text(locs+.02,pks,num2str((1:numel(pks))'))
% Sort the peaks from tallest to shortest.
[psor,lsor] = findpeaks(PeakSig,x,'SortStr','descend');
findpeaks(PeakSig,x)
text(lsor+.02,psor,num2str((1:numel(psor))'))
but I still get the same 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{:});
Error in Testpeask (line 12)
[pks,locs] = findpeaks(PeakSig,x)
does this mean there is something wrong with my matlab?
Thanks for your help

1 Comment

How did you fix this? I'm also stuck with the same error:
Error using findpeaks>parse_inputs (line 288)
Expected a string scalar or character vector for the
parameter name, instead the input type was 'double'.
Error in findpeaks (line 136)
= parse_inputs(isInMATLAB,Yin,varargin{:});
Error in f0est (line 81)
findpeaks(specdb,npartials,'MinPeakWidth',minpeakwidth,'MaxPeakWidth',maxpeakwidth,...

Sign in to comment.

 Accepted Answer

[pks,locs] = findpeaks(PeakSig);
plot(x, PeakSig)
text(x(locs)+.02,pks,num2str((1:numel(pks))'))
Sorted according to height
[pks ind] = sort(pks, 'Descend');
locs = locs(ind);
text(x(locs)+.02,pks,num2str((1:numel(pks))'))

5 Comments

Thanks for your help, with it I managed to get the sample script working, however this still doesn't give me the x values in a vector. I was just looking at my matlab help, and it seems that the findpeaks function no longer supports finding the x value, can you confirm this?
And if so, do you know any other way of finding them with the use of a matlab script (non-manual)
Thanks again for your help
If t is where you want to get the x values from then simply
t(ind);
will map those indices into your t vector to give you the locations.
Like Adam said. In your case you get the x values with
x(locs)
locs give you the index of the peaks, so x(locs) gives you the values of the corresponding x positions.
You are amazing. Thank you very much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!