matlab plots as movie with legend

231 views Asked by At

i have a question regarding legend for movies. This is my code:

fig = figure();
for i = 1: 70000
plot(signal1)
hold on;
plot([i,i],[-5,5])
plot(signal2,'r')
hold off;
title('\fontsize{14} my data');
legend('signal1','signal2');
axis tight;
f(i) = getframe(fig); 
end

The legend shows the same colors for the first two things I plot. if I plot more it works for the other plots. Is there a trick I don't know?

1

There are 1 answers

2
Matt On BEST ANSWER

The strings defined in the legend command are assigned in order of the plots being generated. This means that your first string 'signal1' is assigned to the plot for signal1 and the second string 'signal2' is assigned to the vertical line.

You have two possibilities to fix this problem.

  1. Execute plot for the vertical line after the plot for the two signals.
  2. Use handles to the plots to assign the legends directly.

Here is an example of changing the order:

plot(signal1)
hold on;
plot(signal2,'r')
plot([i,i],[-5,5],'k')
hold off;
legend('signal1','signal2');

Here is an example that uses handles (sp1 and sp2):

sp1 = plot(signal1)
hold on;
plot([i,i],[-5,5],'k')
sp2 = plot(signal2,'r')
hold off;
title('\fontsize{14} my data');
legend([sp1,sp2],'signal1','signal2');