Mark max value and min value with red circle

Hey I need to mark max and min value of this graph with a red circle.
f = importdata("croc.txt");
deviation = f.data(:,1);
dates = f.textdata;
x = datenum(dates, "yyyy/mm");
y = deviation;
figure(1);
plot(x,y)
datetick('x','yyyy')
How can I then mark min and max peak with red circle?

 採用された回答

dpb
dpb 2019 年 9 月 23 日
編集済み: dpb 2019 年 9 月 23 日

2 投票

[~,imx]=max(y);
[~,imn]=min(y);
hold on
plot(x([imn;imx]),y([imn;imx]),'or')
or as I was going to do originally as noted in comment:
ix=[imn;imx];
plot(x(ix),y(ix),'or')

7 件のコメント

Mathias Pettersen
Mathias Pettersen 2019 年 9 月 23 日
I get this error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
dpb
dpb 2019 年 9 月 23 日
Oh,yeah...I was going to make a new variable and then didn't..then didn't add the parens for indexing but just pasted. Adde () around the [] for the indexing expression. Fixed above...
Mathias Pettersen
Mathias Pettersen 2019 年 9 月 23 日
I tried like this
plot(x(indexMin;indexMax),y(indexMin;indexMax),'or')
but didnt work
Also like this:
plot(x(indexMin,indexMax),y(indexMin,indexMax),'or')
but then got error: Index in position 2 exceeds array bounds (must not exceed 1).
dpb
dpb 2019 年 9 月 23 日
Well, you did something wrong...the logic will work if x,y are vectors.
x=1:10; % make up some sample data
y=rand(size(x));
plot(x,y) % plot it
[~,imn]=min(y); % get min/max locations
[~,imx]=max(y);
hold on % hold the plot to add to it
plot(x([imn;imx]),y([imn;imx]),'or') % option one with given locations
ix=[imn;imx]; % second option to build index
plot(x(ix),y(ix),'xk') % plot that way different syhmbol
results in untitled.jpg
that shows both markers at obviously the right locations.
dpb
dpb 2019 年 9 月 24 日
plot(x(indexMin;indexMax),y(indexMin;indexMax),'or')
Indeed, you did do something wrong -- you wrote the subscripting expression as a 2D array expression, not as a vector of 1D indices.
NB: the difference in what wrote and the Answer or example.
PAVARNA TA
PAVARNA TA 2021 年 5 月 17 日
how to mark only the minimum or the maximum point?
dpb
dpb 2021 年 5 月 17 日
Just leave out the one not wanted...

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2019 年 9 月 23 日

コメント済み:

dpb
2021 年 5 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by