Delete line from subplot: Handles not known
이전 댓글 표시
Hello all I would like to know how to delete a line or curve from a subplot for whom the handles are not known(for a saved figure). Consider the following code:
x = rand(10,1);
y = rand(10,1);
figure
subplot(2,1,1);
hold on
plot(x, 'r') % Let's call it plot1
plot(y, 'g') % plot2
subplot(2,1,2);
z = magic(2);
plot(z)
Now I would like to delete plot1. I know that after getting handle for subplot(2,2,1) I can use findobj() to find the red line and remove it, for the purpose I tried:
h1 = get(gcf, 'children');
h2 = get(h1, 'children');
here h1 is 2x1 vector and I do not know which handle belongs to which subplot, also dimension of h2 did not helped as both the cell elements are 2x1(two lines in bot subplots). Any suggestions?
Thanks Pankaj
채택된 답변
추가 답변 (1개)
Sean de Wolski
2014년 12월 3일
편집: Sean de Wolski
2014년 12월 3일
Another thing you can do is set the line's 'ButtonDownFcn' to delete it. Then you can just click on the ones you don't want:
plot(magic(10))
ax = gca;
lines = findobj(ax,'Type','line');
set(lines, 'ButtonDownFcn', @(src,~)delete(src));
Click away!
But I echo dpb's sentiment: it's better to store them and do it the right way. If you have too many variables, start using functions that clean up after themselves to reduce the clutter.
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!