how do I graph the data defined by my if statement?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mackenzie Maher
am 31 Dez. 2021
Kommentiert: Image Analyst
am 1 Jan. 2022
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
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 31 Dez. 2021
Bearbeitet: Image Analyst
am 31 Dez. 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 Kommentare
Image Analyst
am 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);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!