From help:
[X,Y] = meshgrid(-2:.2:2,-2:.2:3);
Z = X.*exp(-X.^2-Y.^2);
[C,h] = contour(X,Y,Z);
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)
colormap cool
Now I want to take only levels equal to 0.4
[X,Y] = meshgrid(-2:.2:2,-2:.2:3);
Z = X.*exp(-X.^2-Y.^2);
[C,h] = contour(X,Y,Z);
set(h, 'LevelList', [ 0.4]);
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)
colormap cool
And now I want to get data that was displayed at figure and display it with 'plot' function.
figure;plot(C(:,1),C(:,2))
How to get data which I needed?

 채택된 답변

Friedrich
Friedrich 2011년 9월 1일

2 개 추천

EDIT: Code now handles different regions correctly.
Hi,
you can use the contour matrix together with inpolygon to get point inside the region:
For example:
hold on
[X,Y] = meshgrid(-2:.2:2,-2:.2:3);
Z = X.*exp(-X.^2-Y.^2);
[C,h] = contour(X,Y,Z);
set(h, 'LevelList', [ 0.1 0.2 0.3]);
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2)
colormap cool
x = reshape(X,1,numel(X));
y = reshape(Y,1,numel(Y));
cm = get(h,'ContourMatrix');
index_start = 2;
index_end = cm(2,1)+1;
IN = inpolygon(x,y,cm(1,index_start:index_end),cm(2,index_start:index_end));
for i=2:numel(get(h,'LevelList'))
index_start = index_end + 2;
index_end = index_start + cm(2,index_start-1) - 1;
tmp = inpolygon(x,y,cm(1,index_start:index_end),cm(2,index_start:index_end));
IN = IN | tmp;
end
plot(x(IN),y(IN),'r+');
hold off
figure
hold on
index_start = 2;
index_end = cm(2,1)+1;
plot(cm(1,index_start:index_end),cm(2,index_start:index_end))
for i=2:numel(get(h,'LevelList'))
index_start = index_end + 2;
index_end = index_start + cm(2,index_start-1) - 1;
plot(cm(1,index_start:index_end),cm(2,index_start:index_end))
end
plot(x(IN),y(IN),'r+')
hold off

댓글 수: 3

Dmitry Borovoy
Dmitry Borovoy 2011년 9월 1일
thanks a lot. That is what I need. But seems like that it works only for one level. There appear some artifacts when I tried to get 2 regions simultaneously
Friedrich
Friedrich 2011년 9월 1일
Thanks for reporting. Fixed it. see modified code above.
K E
K E 2014년 8월 15일
Really useful. Should be built in to the contour program or submitted to Fex.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chad Greene
Chad Greene 2014년 8월 15일

0 개 추천

C2xyz converts the contour matrix to x,y,z values easily.

카테고리

도움말 센터File Exchange에서 Contour Plots에 대해 자세히 알아보기

질문:

2011년 9월 1일

답변:

2014년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by