Update legend to current plot
6 views (last 30 days)
Show older comments
I'm writing a code where I have one constant dataset that stays in the plot the entire time while the other datasets pop up in the plot one by one.(I'm deleting the pop-up dataset-plot after every iteration; which means that I at every time only have two plots in my figure - the constant dataset and the current pop-up dataset). As far as I know you can't have multiple legends in a matlab plot, so I want to make a legend for the pop-up datasets that updates after every iteration (i.e legend changing color in accordance to plot). To delete the plots i'm simply using the delete(plot) command however I haven't figured out if you can do anything similar with legends
0 Comments
Answers (1)
KSSV
on 1 Mar 2016
% constant plot
t = linspace(0,2*pi) ;
x = 0.5*sin(t) ;
for i = 1:100
plot(t,x,'r') ; % plot constant sine function
hold on
% Generating a random varying second function
dx = 0.1*randn(size(t)) ;
x2 = x+dx ; % A varying function
plot(t,x2,'color',rand(1,3)) ; % plot second varying function
hold off
legend('sin function','varying function')
drawnow
end
0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!