??? Subscript indices must either be real positive integers or logicals. Using findpeaks

4 次查看(过去 30 天)
I've have been using findpeaks in MatLab to locate the maximum and minimum points of a waveform with no problem, but in the last 20 minutes or so the error:
??? Subscript indices must either be real positive integers or logicals.
Has appeared an I have no idea why. Even trying simple exercises with test data has resulted in the same error. For example if I were to have the dataset:
test = [ 0.1 0.5 0.9 0.5 0.2 0.6 1.0 0.7 0.3 0.1 ]
and used the code:
peaks = test(findpeaks(test));
I would expect the result:
peaks = [0.1 0.9 0.2 1.0 0.1 ]
but for some reason this is no longer the case.
Please advise.
Thanks,
Jared.
  2 个评论
Jared Rigby
Jared Rigby 2012-4-16
The problem is occuring at: peaks = test(findpeaks(test));
This code previously returned maximum and minimum peak values in a single matrix does now is problematic

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2012-4-16
findpeaks returns the peak, which is 0.9, if you don't accept both output arguments of it. 0.9 in not an integer and so it cannot be used as as index into test. Since you want the peaks, and that's what findpeaks returns, you can simply do this:
testData = [ 0.1 0.5 0.9 0.5 0.2 0.6 1.0 0.7 0.3 0.1 ]
peaks = findpeaks(testData)
and get the results:
testData =
0.1000 0.5000 0.9000 0.5000 0.2000 0.6000 1.0000 0.7000 0.3000 0.1000
peaks =
0.9000 1.0000
These are the peaks, but this does not match what you're calling peaks. It looks like you're including the valleys in how you define peaks. In that case, you might want to run findpeaks again on the inverted data (subtract your data from the max of the data).
maxPeakValue = max(peaks);
% Surround with 0 so you can get the valley
% should it occur at the first or last element.
invertedData = [0, maxPeakValue - testData, 0]
valleys = maxPeakValue - findpeaks(invertedData)

更多回答(2 个)

Richard Brown
Richard Brown 2012-4-16
You've probably declared a variable called findpeaks by accident. Try
which findpeaks
and confirm it's actually pointing to a function
  3 个评论
Jared Rigby
Jared Rigby 2012-4-16
which findpeaks returns:
C:\Program Files (x86)\MATLAB\R2011a Student\toolbox\signal\signal\findpeaks.m
and I don't have anyother variables in my current workspace other than test and peaks

请先登录,再进行评论。


Wayne King
Wayne King 2012-4-16
[pks,locs] = findpeaks(test);
pks
test(locs)
pks contains the peak values
locs are the indices
test(locs)
  5 个评论
Image Analyst
Image Analyst 2012-4-16
He wants the valleys. In my answer I recommended Jared invert the data and run findpeaks again. Valleys will turn into peaks once you've inverted it.
Richard Brown
Richard Brown 2012-4-16
haha, I blame TMW for this confusion (pks vs locs). findpeaks suggests that it returns indices a. la. find, you'd actually want it to be called peaks. But we can't have that, can we!

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by