I have a script in Matlab which finds the peaks and valleys of my data set (looks kinda like a squished sine wave), eventually I want to average all of the data between each peak and trough. Here is the script I am working on, but I keep getting errors when I try to even select out the data between each peak and trough to build a matrix of just the data, even before getting to any of the averaging or anything like that. How can i get this script to work and what am I doing wrong? I get errors like:
The following error occurred converting from cell to double:
Error using double
Conversion to double from cell is not possible.
Error in Test (line 72)
peakAverages(:,j) = {Peakdata((MaxIdx(j):MinIdx(j+1)),:)};
and if I change it to not be a cell array I get the opposite error. The code I am working on is:
for i = 1 : numel (tasknames) %for each test point do the math and find the peaks. tasknames lists the test points.
TaskData = cell2mat (task_data(i));
Peakdata = TaskData(:, 4); %data is in the 4th column of my larger data matrix.
[Maxima, MaxIdx] = findpeaks (Peakdata, ...
'MinPeakHeight', mean (Peakdata), ...
'MinPeakDistance', 10);
Maxima = Peakdata (MaxIdx);
Troughdata = 1.01 * max (Peakdata) - Peakdata;
[Minima, MinIdx] = findpeaks (Troughdata, ...
'MinPeakHeight', mean (Troughdata), ...
'MinPeakDistance', 10);
Minima = Peakdata (MinIdx);
MinLength = length (MinIdx);
MaxLength = length (MaxIdx);
%if there is a trough first, I want to take the first peak value
%index and the second trough value index. Also since there are
%different numbers of peaks and troughs, I want to make sure that
%they match in length by always ending on a trough index.
%if there is a peak first, or MinIdx is greater than MaxIdx, do
%normal j = 1 until whichver list of peaks/trouhgs is shorter.
if MinIdx(1) < MaxIdx(1)
if MinLength > MaxLength
for j = 1 : length (MaxIdx)
peakAverages(:, j) = {Peakdata((MaxIdx(j) : MinIdx(j + 1)), :)};
end
elseif MinLength == MaxLength
for j = 1 : length (MinIdx)
peakAverages(:, j) = {Peakdata(MaxIdx(j) : MinIdx(j + 1), :)};
end
elseif MinLength < MaxLength
for j = 1 : length (MinIdx)
peakAverages(:, j) = {Peakdata(MaxIdx(j) : MinIdx(j + 1), :)};
end
end
else
if MinLength > MaxLength
for j = 1 : length (MaxIdx)
peakAverages(j, :) = {Peakdata(MaxIdx(j) : MinIdx(j), :)};
end
elseif MinLength == MaxLength
for j = 1 : length (MinIdx)
peakAverages(:, j) = {Peakdata(MaxIdx(j) : MinIdx(j), :)};
end
elseif MinLength < MaxLength
for j = 1 : length (MinIdx)
peakAverages(:, j) = {Peakdata(MaxIdx(j) : MinIdx(j), :)};
end
end
end