MATLAB Answers

0

Legend for plot genereted by a loop

Asked by Kelly Kyriakou on 8 Oct 2015
Latest activity Commented on by Kelly Kyriakou on 10 Oct 2015
I want to plot 2 variables and each row of the 2 variables to be presented with different color. So I wrote the following code which plot through loop:
plot(Xindex2plot,Yindex2plot,'color',cc(allSS,:));
However, I have problem with the legend. At legend it is presented only the color of the last row of data and the text respectively. This is the code for the lgend which it is supposed that produced through loop also.
% s{allSS}=sprintf('%s',nameofsource2open);
% legend(plotall,s)
How I should reform it to present all the data and the texts?
Thank you in advnace

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Thorsten
on 8 Oct 2015
 Accepted Answer

Store a handle to each plot in h(i) and let the legend refer to this handle h:
col = rand(3, 3);
data = rand(3, 100);
axis, hold on, for i=1:3, h(i) = plot(data(i,:), 'Color', col(i,:)); end
legend(h, {'data1' 'data2' 'data3'})

  4 Comments

Show 1 older comment
In the loop:
legendtext{i} = variable_that_changes;
and then
legend(h, legendtext)
Thank you very much!!!
It works!!! Thank you very much

Sign in to comment.