Duplicating data name when using legend in a for loop

6 次查看(过去 30 天)
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 个评论
Avishek  Mondal
Avishek Mondal 2017-7-9
Hey Walter, thanks for that! I have changed my evals to doubles. When is the best case to use eval?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-7-8
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 个评论
Avishek  Mondal
Avishek Mondal 2017-7-9
No worries, I believe I have managed to solve it. Here is basically what I did-
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)));
l{i} = strcat('Q= ', num2str(Q(i)));
p{i} = plot(imag(solTheta)/pi,om/omega_0, lineCol(i));
hold on;
end
p_l = [p{1}(1) p{2}(1) p{3}(1)];
leg = legend(p_l, l);
xlim([0 0.5]);
ylabel('\omega/\omega_0');
xlabel('\alphaa/\pi');
t =['Imaginary part, \kappa = ', num2str(kap)];
title(t);
Walter Roberson
Walter Roberson 2017-7-9
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by