I've been thinking how I could update in Matlab the legend of my plots as the for goes on, basically, I have a for
which creates a graph that is added to the plot
(using hold on
) in every iteration, I'd like to update the legend of the aforementioned plot.
This is how I've done it:
clear all; close all; clc;
x0 = 10;
t = linspace(0, 2, 100);
v0 = 0;
g = [10:10:100];
str = [];
hold on
for i = 1:length(g)
x = x0 + v0*t - 1/2*g(i)*t.^2;
v = v0 - g(i)*t;
plot(t, x)
axis([0, 2, -200, 10]);
str = [str, sprintf("g = %d", g(i))];
legend(str, 'Location','southwest');
pause(0.3);
end
hold off
i.e. using that size-changing string str. I have a feeling that there is a better and more performant way of doing this but I don't know how else to approach the problem.