Duplicating data name when using legend in a for loop
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 8 Lug 2017
You should never eval() a symbolic expression. Symbolic expressions are in a language which is close to MATLAB but not exactly the same. You should subs() if necessary to give values to any variables, and you should double() what is left if you need a floating point numeric value.
Avishek Mondal
il 9 Lug 2017
Walter Roberson
il 9 Lug 2017
The best case to use eval is... basically, never.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Legend in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!