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

20 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2016-9-16
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 个评论
jg
jg 2016-9-16
Excellent, thanks! I forgot about the findobj command. If that ends up being slow I'll use the text command modification you listed.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by