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

조회 수: 5 (최근 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 CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by