Is there an easy way to remove a plotted line from a set of axes without clearing everything else on the axes? I'm trying to implement a GUI with a listbox containing several data sets. I can make the callback function plot the selected data, but I'm not sure how to 'unplot' it when I deselect a data set. Any ideas?
Asked
Active
Viewed 3.4k times
11
-
3you might be interested in `clickableLegend` on FEX: http://www.mathworks.com/matlabcentral/fileexchange/21799-clickablelegend – Amro Aug 02 '10 at 19:57
-
@Amro: Excellent suggestion! Thanks for bringing this to my attention. – Doresoom Aug 02 '10 at 20:08
1 Answers
17
If you save a handle to the created graphics object, you can call DELETE on it to remove it from the plot:
hLine = plot(...); %# Create a line with PLOT
delete(hLine); %# ...and delete it
Alternatively, if you didn't save the handle in a variable, you can search for it using FINDOBJ, then delete it when you find it.
If you don't actually want to delete it, but simply turn the visibility of the line on and off, you can set the 'Visible'
property of the graphics object accordingly:
set(hLine,'Visible','off'); %# Make it invisible
set(hLine,'Visible','on'); %# Make it visible

gnovice
- 125,304
- 15
- 256
- 359
-
-
-
1@Doresoom: That explains it. The command syntax `delete plothandle` would be looking for a *file* named `plothandle` to delete. – gnovice Aug 02 '10 at 14:47
-
3
-
-
@RupertJones, true, but this will fix the legend: `legend('off'),legend('show')` – Juhl Sep 22 '11 at 08:19
-
@Juhl: Thanks, but I meant the individual legend for the plot. I only figured out the way by hiding each individual component (text, line, ...) of the respective legend item. – Mathias Soeken Sep 23 '11 at 17:24
-
@RupertJones I've switched from legend(...) towards plot(..., ,'DisplayName',{'V_x','V_y','V_z'}) which will keep annotations for each line. – Juhl Sep 26 '11 at 13:28