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

2 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 31 Dec 2021
Edited: Image Analyst on 31 Dec 2021
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 Comments
Mackenzie Maher
Mackenzie Maher on 1 Jan 2022
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 on 1 Jan 2022
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);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!