MATLAB: Legend doens’t work

legend

I want to have 2 subplots with each 2 lines. One is RL and one is RC. But when I try to make a legend he only adds the RC legend to the line of the RL.
f = logspace (0, 6, 100); % vector of frequencies in log scale
R = 100; L = 1e-3; % parameters of the RL circuit
H_RL = (1i*2*pi*f*L./R)./(1+1i*2*pi*f*L./R); % transfer function (vector); use the correct equation

A = abs (H_RL); % amplitude-transfer (vector)
dFi = angle (H_RL); % fase difference (vector)
subplot (211)
legend('RL')
% create graph of amplitude response, A in [dB]

semilogx (f, 20 * log10 (A))
grid on
xlabel ('frequency [Hz]')
ylabel ('response [dB]')
subplot (212)
legend('RL')
% Create graph with phase shift, dFi in [degrees].

semilogx (f, 180 ./pi * dFi)
ylim ([-180 180])
grid on
xlabel ('frequency [Hz]')
ylabel ('phase shift [deg]')f = logspace(0, 6, 100); % vector of frequencies in log scale, omega = 2 * pi * f
R = 100; C = 1e-4; % parameters of the RC circuit
H_RC = 1./(1+2*pi*f*R*C*1i); % transfer function (vector); use the correct equation
A = abs (H_RC); % amplitude response (vector)
dFi = angle (H_RC); % fase shift (vector)
subplot (211)
legend('RC')
hold on
% create graph of amplitude response, A in [dB]
semilogx (f, 20 * log10 (A))
grid on
xlabel ('frequency [Hz]')
ylabel ('response [dB]')
subplot (212)
legend('RC')
hold on
% Create graph with phase shift, dFi in [degrees].
semilogx (f, 180 ./pi * dFi)
ylim ([-180 180])
grid on
xlabel ('frequency [Hz]')
ylabel ('phase shift [deg]')
Maybe somebody knows how to fix this?

Best Answer

You start by calling legend('RL') in subplot 211 before you have plotted anything in subplot 211. That would probably give a warning "Warning: Ignoring extra legend entries." An empty legend would be created if you are using R2017b or later, but in releases before that, in this circumstance of the axes being empty, no empty legend would be created.
Then you plot something in subplot 211. Because "hold" is not on in subplot 211, the plotting triggers the axes being cleared including removing the empty legend (if it was there at all.)
Then you switch to subplot 212 and call legend('RL') there before you have plotted anything in subplot 212. That would probably give that warning about extra legend entries. An empty legend would be created if you are using R2017b or later, but in releases before that, in this circumstance of the axes being empty, no empty legend would be created.
Then you plot something in subplot 212. Because "hold" is not on in subplot 212, the plotting triggers the axes being cleared including removing the empty legend.
Then you switch back to subplot 211 and call legend('RC') there in subplot 211. There is an existing line in that subplot so the that line (which was the RL line) would be labeled with the RC legend you just created.
Then you "hold on" in subplot 211, and then you plot RC there. In versions before R2017a, this creation of a new line in the subplot would not result in a second legend entry automatically being created; in R2017a and later, adding the second line to the subplot would result in a second legend entry automatically being added with legend 'data1'.
Then you switch back to subplot 212 and call legend('RC') there in subplot 212. There is an existing line in that subplot so the that line (which was the RL line) would be labeled with the RC legend you just created.
Then you "hold on" in subplot 212, and then you plot RC there. In versions before R2017a, this creation of a new line in the subplot would not result in a second legend entry automatically being created; in R2017a and later, adding the second line to the subplot would result in a second legend entry automatically being added with legend 'data1'.
We can deduce from your question that you are using a version of MATLAB before R2017a.
In versions before R2017a, you should not legend() entries into existence until you have already drawn the lines you want to label, and in those versions the only way to add new entries is to call legend() again with all of the labels for that axes (though there might have been one obscure way to add more legends involving setting DisplayName properties and calling legend with a particular keyword.)
There is more flexibility for adding legend entries dynamically in R2017a and later, but you still need to know a bit of magic.