Overwrite data in figure but keep axis labels, title, and legend

Hi,
I would like to overwrite only the data presented in a figure while keeping the labels and legends the same. I do not want to waste space and time on re-labeling the figures. 'hold on' holds the plot but does not overwrite the existing data and only adds to it.
Thank you for your help.

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 7월 15일
h=plot(rand(1,100));
title('random signal');
legend('blue signal');
set(h,'XData',1:10,'YData',1:10);

추가 답변 (2개)

Jack LeBien
Jack LeBien 2016년 8월 6일
Hi, you can use
hold on
cla
plot(newData)
cla (clear all) will remove the plotted data but not other formatting.

댓글 수: 7

That doesn't solve the OP's problem. I just tried this (because I have the exact same question as OP), but when you subsequently use the plot command again, it removes the axis labels and etc. The goal here was to preserve the other formatting but "refreshing" the plotted data to something else.
Jack LeBien
Jack LeBien 2016년 8월 12일
편집: Jack LeBien 2016년 8월 12일
I didn't have that issue. If I have a figure with certain labels and other formatting active, then set hold on, cla will then clear the plotted data, leaving labels etc. Then plot will fill the plot without clearing the formatting. Did you make sure to have hold on?
I did this technique helpful, thanks.
wow this is the most simple way to solve my similar problem!
thanks !!!!!!!!!!!!
This should be the accepted answer.
+1 Dyl, I think as well that this one should be accepted.
Thanks!!
using cla instead of clf works well

댓글을 달려면 로그인하십시오.

MechtEngineer
MechtEngineer 2014년 4월 8일
편집: John Kelly 2022년 10월 31일
I don't see how the previous answer keeps the axis labels, title and legend when plotting some new data on the same axis.
The code that worked for the author (and for me) was:
title('Some Title','HandleVisibility','off');
ylabel('Some ylabel','HandleVisibility','off');
xlabel('Some xlabel','HandleVisibility','off');
set(gca,'NextPlot','replacechildren') ;
Any subsequent plots on this axes keeps the existing axes properties (including title, axis labels and legend), while replacing child objects (e.g. data)

댓글 수: 2

this method proves to be good, which can solve my problem, i want to keep the label in matlab gui while ploting in callback functions. thanks guys
tried only set(gca,'NextPlot','replacechildren') ; works good too

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Labels and Annotations에 대해 자세히 알아보기

제품

질문:

2011년 7월 15일

편집:

2022년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by