Clearing only the data in axes (while preserving text labels)

27 views (last 30 days)
I am making a ton of plots in a loop with the same format and labeling. Instead of creating a new figure for each set of data, I am simply clearing out the previous data and plotting the next data (much faster this way, especially since my figures have subplots).
How do I clear just the data ("Line" objects) without clearing the other stuff on the plots (e.g. labels made with the text command)?
If I type
get(gca,'children')
I get both the data ("Line") and the text label ("Text"). The cla function clears both of these, so that doesn't work. The other option I know of is to use delete() on the plot handle, i.e.
ph = plot(x,y)
delete(ph)
However, each figure I make has a different number of lines plotted on it, so I don't know how to keep track of a variable number of plot handles. Is there some way just to clear the data while preserving the text? I really don't want to remake the text every time.
I am running 2015b. That brings up a related question - in 2016b you can give the text command the axes handle text(handle,x,y,string). Is there any way to do that in 2015b without running the axes command to get the current axes? If not, and there's no way to clear the data from a plot as described above, then I'm kind of stuck... using the axes command to rewrite the labels for every figure is pretty slow.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Sep 2016
ax = gca; %or you might have stored it earlier (Hint!)
delete( findobj(ax, 'type', 'line') )
text(x, y, z, 'TheString', 'Parent', ax) %not documented specifically in 2015b but accepted
  1 Comment
jg
jg on 16 Sep 2016
Excellent, thanks! I forgot about the findobj command. If that ends up being slow I'll use the text command modification you listed.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!