Delete line from subplot: Handles not known
Show older comments
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
Accepted Answer
More Answers (1)
Sean de Wolski
on 3 Dec 2014
Edited: Sean de Wolski
on 3 Dec 2014
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.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!