Delete line from subplot: Handles not known
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
dpb
el 2 de Dic. de 2014
You determine which set of handles belong to which axes by using subplot(M,N,p) to set the p axes as the current axes before retrieving the handles. In this case, use findobj to find the line handle under that axes handle with the linecolor red.
subplot(2,1,1) % make first the current axes
findobj(gca,'color','r')
Of course this teaches the useful lesson it's better to save the handles when creating objects... :)
3 comentarios
dpb
el 3 de Dic. de 2014
...the handles are not saved intentionally...
I'd consider that bad practice; a few extra doubles or arrays of a half-dozen elements is nothing in today's memory footprint as compared to the complexity and overhead of using findobj or the like. Plus, it's far more error prone and likely to give support problems later on by having to remember "what the heck it was I was trying to do here?" a few weeks (or year?) from now.
Plus, if you factor, once the routine finishes its work, unless your app is going to continue to mung upon the figure later(*), all local variables will disappear automagically anyway upon exit of the function, thereby clearing the workspace. If it's a script and you're cluttering the main workspace, turn it into a function even if it takes no argument and returns no result.
() If this is the case, then it's even more important to save the actual handles as relying upon *gca and/or gcf can very easily leave you working on the wrong object.
...also I want to learn new tools.
That's commendable but don't "fall in love" with complexity for the sake of it. Use the "deadahead" simple technique first. While it's good to know how to "handle-dive" via findobj and friends, try to avoid it as general practice unless is "the only way"...
Más respuestas (1)
Sean de Wolski
el 3 de Dic. de 2014
Editada: Sean de Wolski
el 3 de Dic. de 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.
Ver también
Categorías
Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!