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?

 Accepted Answer

dpb
dpb on 23 Sep 2019
Edited: dpb on 23 Sep 2019
[~,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 Comments

I get this error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
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...
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).
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.
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.
how to mark only the minimum or the maximum point?
Just leave out the one not wanted...

Sign in to comment.

More Answers (0)

Tags

Asked:

on 23 Sep 2019

Commented:

dpb
on 17 May 2021

Community Treasure Hunt

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

Start Hunting!