필터 지우기
필터 지우기

Keeping previous legend in multiple plots done by a function

조회 수: 1 (최근 30일)
PatrizioGraziosi
PatrizioGraziosi 2021년 3월 5일
댓글: PatrizioGraziosi 2021년 6월 1일
Hello everybody,
I have a function, say my_plot_function , which plots simultion results. If I run my_plot_function more times, I can plot the different data on the same figure, however the legend is not kept and only the legend for the last my_plot_function run is displayed.
I want to hold not only the previous plots, but also the previous legend, and the plots are done by launching my_plot_function .
How can I keep also the previous legends, in addition to the plot?
Thanks
Patrizio

답변 (1개)

Monisha Nalluru
Monisha Nalluru 2021년 3월 8일
편집: Monisha Nalluru 2021년 3월 8일
You can use hold method to retain the current plot while adding new plot. Legend method to add legends to axes
Here is an example of retaining legend on the axes while calling function to plot new data
function my_plot(x,y,legendName)
plot(x,y,'DisplayName',legendName);
legend
end
x=linspace(1,10);
y=x*2;
y1=x*3;
y2=x*4;
my_plot(x,y,'x*2');
hold on
my_plot(x,y1,'x*3');
my_plot(x,y2,'x*4');
hold off
Hope this helps!
  댓글 수: 2
PatrizioGraziosi
PatrizioGraziosi 2021년 6월 1일
Unortunately this does not work in a GUI app.
I circumvented the issue by doing as follows. This way, I re-load and re-apply the legend
leg_all = findobj(gcf, 'Type', 'Legend');
leg = leg_all.String';
labels = cellstr(labels);
leg_lab = [leg ; labels] ;
A = 'data' ;
N = ~cellfun('isempty',strfind(leg_lab,A) ) ;
leg_lab(N)=[];
legend(leg_lab);
PatrizioGraziosi
PatrizioGraziosi 2021년 6월 1일
Howvwer, this works only when I use theGUI to plot in an external figure. If I want to plot in a graph inside the GUI app, after two or three plots it automatically reset the lines...

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

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by