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

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...
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 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 Exchange에서 Operators 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