How do I plot all values in my IF loop?

13 ビュー (過去 30 日間)
ImperialStar
ImperialStar 2019 年 12 月 26 日
コメント済み: Voss 2022 年 6 月 13 日
I am writing a function, which cannot use the findpeaks or islocalmax, to plot all the maximum values on a graph. However with my if loop it plots each maximum value on a different figure
How do I display them all on one figure? Help very much appreciated.
Here is my function:
function [k] = Peak(X,Y)
for k = 2:length(Y)-1
if (Y(k) > Y(k-1) & Y(k) > Y(k+1) & Y(k) >100)
figure
plot(X,Y,X(k),Y(k),'r*')
end
end
end
  2 件のコメント
Monica Mahesh
Monica Mahesh 2022 年 6 月 13 日
編集済み: Monica Mahesh 2022 年 6 月 13 日
I see blank plot sheet after running this.Can you please help me to get the plot in this program
for k=0:M:M*N;
x=k+y;
m=2;
plot(x,m,'k')
hold on;
end
for k=2:M:M*N;
x=k+y;
m=0;
plot(x,m,'k')
hold on;
end
hold off;
axis([0 12 -0.5 2.5])
Voss
Voss 2022 年 6 月 13 日
@Monica Mahesh: You are plotting one point at a time, in which case you'll need a data marker to see the points:
y = 1; % I make up some values here
M = 2;
N = 5;
for k=0:M:M*N;
x=k+y;
m=2;
plot(x,m,'*k'); % use marker '*' for instance
hold on;
end
axis([0 12 -0.5 2.5])
Or, if you want to plot a line to connect those points, you can plot them all at once:
figure();
x = (0:M:M*N)+y;
m = 2*ones(1,numel(x));
plot(x,m,'k')
axis([0 12 -0.5 2.5])

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

採用された回答

Image Analyst
Image Analyst 2019 年 12 月 26 日
編集済み: Image Analyst 2019 年 12 月 26 日
Instead of the call to figure, call subplot() instead.
rows = ceil(sqrt(length(Y) - 2)); % Can put this line outside the loop
subplot(rows, rows, k-1); % In the loop, replace figure with this line.
This will make an array of several graphs (axes) all on the same figure.
If you want them all on the same graph/axes, then don't call subplot() at all, but call hold on after plot, and use && instead of &.
function [Peak] = findLocalMaxThreshold(X,Y)
for k = 2:length(Y)-1
if (Y(k) > Y(k-1) && Y(k) > Y(k+1) && Y(k) >100)
plot(X,Y,X(k),Y(k),'r*')
hold on;
end
end
hold off;
end
  1 件のコメント
ImperialStar
ImperialStar 2019 年 12 月 26 日
This is perfect thank you very much

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by