Duplicating data name when using legend in a for loop
6 views (last 30 days)
Show older comments
Avishek Mondal
on 8 Jul 2017
Commented: Walter Roberson
on 9 Jul 2017
I've got the following code- it is very rough, because I was trying to compare the effect of different Qs.
close all;
clear all;
syms omega L C M Q theta f_0;
f_0 = 1e6;
Q = [10 100 1000];
omega_0 = 2*pi*f_0;
M = -0.1;
L = 2;
C = 1/(L*omega_0^2);
kap = 2*M/L;
% R=omega_0*L./Q;
% theta = beta - 1*alpha;
figure;
lineCol = ['r', 'b', 'g'];
om = linspace(0.01,1.5,1000)*omega_0;
l = strings(size(Q));
hold on;
for i = 1:3
eqn1 = 1 - (omega_0/omega)^2-(1i*omega_0/(omega*Q(i)))+kap*cos(theta) == 0;
w = solve(eqn1, theta);
solTheta = eval(subs(w,omega,om));
% s = strcat('Q = ', num2str(Q(i)));
s = ['Q= ' num2str(Q(i))];
plot(real(solTheta)/pi,om/omega_0, lineCol(i),'DisplayName',s);
end
legend('show');
xlim([0 1]);
ylabel('\omega/\omega_0');
xlabel('\betaa/\pi');
t =['Real part, \kappa = ', num2str(kap)];
title(t);
figure;
for i = 1:3
eqn1 = 1 - (omega_0/omega)^2-(1i*omega_0/(omega*Q(i)))+kap*cos(theta) == 0;
w = solve(eqn1, theta);
solTheta = eval(subs(w,omega,om));
s = strcat('Q = ', num2str(Q(i)));
plot(imag(solTheta)/pi,om/omega_0, lineCol(i),'DisplayName',s);
hold on;
end
legend('show');
xlim([0 0.5]);
ylabel('\omega/\omega_0');
xlabel('\alphaa/\pi');
t =['Imaginary part, \kappa = ', num2str(kap)];
title(t);
The legend is duplicating the datanames, and I do not know how to solve it. I've attached the plot I'm getting. When I use simple data, like in the following standalone code, I get the legend to perform just fine!
hold on
for p = 1:5
name = ['Line Number ' num2str(p)];
plot(1:10,(1:10)+p, 'DisplayName', name)
end
legend show
Can someone tell me what is wrong?
3 Comments
Accepted Answer
Walter Roberson
on 8 Jul 2017
The result of w = solve(eqn1, theta) is a pair of solutions. When you eval(subs(w,omega,om)) you get a result which is 2 rows by 1000 columns. Then when you
plot(imag(solTheta)/pi,om/omega_0, lineCol(i),'DisplayName',s);
you are plotting with two rows of x and a single row of y. That is going to give you two output lines.
The two solutions, w, are negatives of each other.
When you plot, one of the two of them is not visible because you used an xlim() starting from 0. But the line is still there so it is still present in the legend.
3 Comments
Walter Roberson
on 9 Jul 2017
Instead of
solTheta = eval(subs(w,omega,om));
You can use
solTheta = abs( double(subs(w(1),omega,om)) );
The abs() is to get around the question of whether it happens to be w(1) or w(2) which is the positive branch.
Anyhow with this you would not need to record the handles and so on.
Recording the handles and selective legend() is a good tool, very useful sometimes... it just happens you do not need it this time.
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!