Previous legend replaces the new one in a plot, why?

5 ビュー (過去 30 日間)
Raju Kumar
Raju Kumar 2022 年 8 月 19 日
コメント済み: Kevin Holly 2022 年 8 月 22 日
In an app (desgined using Matlab), I plot x and y-data and clear axes using 'cla' in order to plot a different x-y data. The issue is- the legend from the previous re-appears on the new graph replacing the new legend. I can not understand why. I want to completely remove the legend for the first dataset and prepare the same axes for a new data. I have tried using other functions as well but it does not work. Any help would be appreciated. Thanks a lot.

回答 (1 件)

Kevin Holly
Kevin Holly 2022 年 8 月 19 日
編集済み: Kevin Holly 2022 年 8 月 19 日
Were you changing the location of the legend as shown below? I made a quick app (attached) using the approach below.
plot(rand(10,1),rand(10,1))
hold on
plot(rand(10,1),rand(10,1))
legend({'example';'example2'},"Location","west")
cla
plot(rand(10,1),rand(10,1))
If that is the case, you can create a handle for the legend and delete it after clearing axes.
figure
plot(rand(10,1),rand(10,1))
hold on
plot(rand(10,1),rand(10,1))
L=legend({'example';'example2'},"Location","west");
cla
delete(L)
plot(rand(10,1),rand(10,1))
legend
  2 件のコメント
Raju Kumar
Raju Kumar 2022 年 8 月 22 日
Thanks very much for your comment. No, I do not change the location. What I do is just clear the axes using cla which makes the plot and legend disappear, but when I plot a different data on the same cleared axes, the previous legend appears at the same location, not the new one. I checked with this delete function but it did not help.
Raju
Kevin Holly
Kevin Holly 2022 年 8 月 22 日
@Raju Kumar What version of MATLAB are you using?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by