I used the following code to generate my plot (and I want it later to include in my LateX document):
clear all; close all; clc;
a1 = 1; a2 = 1; c1 = 2.0; c2 = 1.8; time = 0:0.1:300;
wave1 = a1 * sin(c1*time);
wave2 = a2 * sin(c2*time);
wave3 = wave1 + wave2;
y = hilbert(wave3);
env = abs(y);
bound = 0.1*cos(0.2*time-pi);
plot(time,wave3,'k',time,[-1;1]*env,'--k',time,bound,'-.r', 'Linewidth',1.2);
ylabel(' $\eta$ (m)');
xlabel(' Time (s)');
legend = legend({'Short waves','Wave group envelope','Bound long wave'});
set(legend, 'FontSize',20);
axis([15.7 110 -2.5 2.5]);
The graph looks like:
Clearly, the 'bound long wave' legend does not match the color and line specification in the graph. As far as I know, it has something to do with scalar/vector but I cannot figure out where the error is.
How to proceed further?
You are actually creating four plot objects but only providing a legend for three of them. If we break down your plot
statement we can count the plots (keeping in mind that plot
called with a matrix plots each column as a different plot).
plot(time, wave3, 'k', ... <--- This is one plot
time, [-1;1]*env, '--k', ... <--- This is TWO plots (one negative, one positive)
time, bound, '-.r', ... <--- This is one plot
'Linewidth',1.2);
When you call legend
with only three labels, it is only going to create a legend entry for the first three plot objects.
Since you probably don't want two entries for the envelope, you should assign the output of plot
to a variable and pass the handles explicitly to the legend
.
p = plot(time, wave3, 'k', ...
time, [-1; 1] * env, '--k', ...
time, bound, '-.r', ...
'LineWidth', 1.2);
% Skip the third plot since that will just be the "negative" envelope
legend(p([1 2 4]), {'Short waves','Wave group envelope','Bound long wave'});