MATLAB: How to dynamically allocate labels to a legend

datafor looplegendplotplotting

I want to dynamically assign a legend to a data set with a changing number of vectors (v_1, v_2,…,v_n) that I plot against a single vector A. The vectors v_n are stored in a cell array, T{1,n}.
Some psuedo-code would be something like,
n=5
for i=1:n
plot(A,T{1,i})
legend('X=',i) % Essentially I want something like, legend('X=1', 'X=2', 'X=3', 'X=4', 'X=5',..., 'X=n')
end
So in this example by setting the value of n to be 5, I should get 5 different plots on my graph with a legend that displays X=1 through to X=5.
Is there a way to do this?
Thank you!

Best Answer

n=5 ;
lgd = cell(n,1) ;
figure
hold on
for i=1:n
plot(rand(10,1)) ;
lgd{i} = strcat('X=',num2str(i)) ;
end
legend(lgd)
Related Question