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

14 ビュー (過去 30 日間)
jg
jg 2016 年 9 月 16 日
コメント済み: jg 2016 年 9 月 16 日
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 Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by