how do I graph the data defined by my if statement?

1 回表示 (過去 30 日間)
Mackenzie Maher
Mackenzie Maher 2021 年 12 月 31 日
コメント済み: Image Analyst 2022 年 1 月 1 日
Hi everyone,
I have the following code and im trying to figure out how to graph the data defined by my if statement to make sure I'm actually choosing the data I want. I thought the following would work but all it produces is a blank graph.
Any suggestions on how to fix this would be fantastic!
Thanks
fieldNames = fieldnames(MCR_full.MIB037.Reaches);
allPeaks = cell(10,1);
for k = 1 : 10
thisFieldName = fieldNames{k};
thisArray = MCR_full.MIB037.Reaches.(thisFieldName).kin;
x = thisArray(:, 1);
y = thisArray(:, 3);
peaks = findpeaks(y);
title(['Peaks for ', thisFieldName]);
hold on;
grid on;
allPeaks{k} = peaks(:).';
reachLimitArray= find (peaks >= 0.12);
if length(reachLimitArray )> 1
disp('There is at least two value above the limit.');
for i = 1 : length(reachLimitArray)
index = reachLimitArray(i);
disp(peaks(index));
end
else
disp('All values are below the limit.');
end
celldisp(allPeaks);
end

採用された回答

Image Analyst
Image Analyst 2021 年 12 月 31 日
編集済み: Image Analyst 2021 年 12 月 31 日
Perhaps this:
for k = 1 : length(fieldNames) % For every fieldname we have ...
% Get the name of the kth field.
thisFieldName = fieldNames{k};
% Extract the data from the structure.
thisArray = MCR_full.MIB037.Reaches.(thisFieldName).kin;
x = thisArray(:, 1);
y = thisArray(:, 3);
% Find peaks in y.
[peakYValues, indexesOfPeaks] = findpeaks(y);
%---------------------------------------------------------------
% PLOTTING CODE:
% Plot curve y vs. x.
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
% Plot red triangles atop each peak.
hold on;
plot(x(indexesOfPeaks), peakYValues, 'rv', 'LineWidth', 2, 'MarkerSize', 12);
caption = sprintf('Peaks for %s', thisFieldName)
title(caption, 'FontSize', 18);
drawnow;
pause(0.3); % Pause long enough to see it before the next field gets plotted.
% The rest of your code in the loop follows ...
end
  3 件のコメント
Mackenzie Maher
Mackenzie Maher 2022 年 1 月 1 日
One more question I promise! if I wanted to plot [pks,locs,w,p]=findpeaks(y)instead of indexesOfPeaks how would I do that? I Tired just swapping the 2 commands but got the following error
Error using plot
Not enough input arguments.
Again Thanks so much!
Image Analyst
Image Analyst 2022 年 1 月 1 日
I don't really like using short, cryptic variable names like that. I think the code is much more maintainable if you use more descriptive variable names like I did. Now anyone coming along later, when they see this in your code:
[pks, locs, w, p] = findpeaks(y)
won't know what pks is. They might guess it means "peaks", but is it the actual values of the signal at the peak locations, or is it the indexes of the peaks, or the x values of the peaks? And is locs the x values at the peaks, or the indexes of the x values at the peaks? So they'll have to consult the documentation. With my variable names, it was completely unambiguous.
But if you insist:
[pks, locs, w, p] = findpeaks(y)
plot(x(locs), pks, 'rv', 'LineWidth', 2, 'MarkerSize', 12);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by