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?

 Akzeptierte Antwort

dpb
dpb am 23 Sep. 2019
Bearbeitet: dpb am 23 Sep. 2019

2 Stimmen

[~,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 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 23 Sep. 2019

Kommentiert:

dpb
am 17 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by