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!