how do I graph the data defined by my if statement?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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 个评论
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 Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!