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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by