我在Matlab中有一个脚本,可以找到我的数据集的峰值和波谷(看起来有点像一个压扁的正弦波),最后我想平均每个峰值和谷值之间的所有数据。这是我正在处理的脚本,但是当我尝试甚至在每个峰和谷之间选择数据来构建数据矩阵时,我仍然会遇到错误,甚至在进行任何平均或类似的事情之前。我怎样才能使这个脚本工作,我做错了什么?我得到的错误如下:
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)),:)};
如果我将其更改为不是单元格数组,则会出现相反的错误。我正在处理的代码是:
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
答案 0 :(得分:0)
由于你并不热衷于创建一个简单的工作示例来重现错误以将事情置于上下文中,所以我自己制作了一个玩具:
% got rid of the for loop - not needed for a minimal example
Peakdata = sin([-50:50]).';
% remaining code is the same.
我无法重现您的错误。它只是工作。
因此,您的错误位于Peakdata
的定义之前或之前的某个位置。
如果您认为不是这样,那么请提供更多appropriate example。即一些短的(例如&lt; 10行代码)和可重复的东西,这样我就可以复制/粘贴到我的matlab会话上并运行并自己检查错误。否则你的问题的答案是“不,它有效,错误在其他地方”。