# How to make different character for each legend in my code?

2 views (last 30 days)
mehmet salihi on 1 Jan 2022
Hi everyone,
I am trying to give each line different legend charcter like * or o to distinguish better among them.
like the legend shapes as shown here
close all
clear all
clc
%%% Fixed Parameters
B = 2; % Number of Blades
Rho = 1.225; % Density of air kg/m3
w = 7.501; % Rotational Speed rad/s
Theta_P = -3; % Tip Pitch Angle in degree
ac = 0.2; % Glauert correction
eps = 1e-5; % Error tolerance
nc=1:1:16; % number of configurations for effect off different distribution
%%% Airfoil Date
Aoa = afoil(:,1);
Cl = afoil(:,2);
Cd = afoil(:,3);
Velocity =[5,6,7,8,9,11]; % Wind Speed m/s
MTotal=[];
%%% Torque vs Velocity Data
vupper=torquee(1:5,1); % Radial Distance in meter
vlower=torquee(6:10,1); % Chord length in meter
torquelower=torquee(1:5,2); % Twist in degree
torqueupper=torquee(6:10,2);
sections = readmatrix('chord and twist 15 distributions.xlsx');
iter=0;
for k=3:2:33
iter=iter+1; % used in legends in plots
r=sections(1:26,2); % Radial Distance in meter
SS=sections(1:26,3); % Span Station %
Chord=sections(1:26,k+1); % Chord length in meter
Beta=sections(1:26,k+2); % Twist in degree
for j=1:length(Velocity)
for i = 8:length(r)
sigma(i) = (B*Chord(i))/(2*pi*r(i));
a=0;
a_prime=0;
alist(i)=[0];
alistprime(i)=[0];
for n=1:1000
Phi = atand(((1-a)*Velocity(j))/((1+a_prime)*w*r(i))); % Flow Angle
Theta = Beta(i) + Theta_P ;
aoa = Phi- Theta; % local angle of attack in degree
f = (B/2)* ((R-r(i))/(r(i)*sind(Phi)));
F = (2/pi)*acos(exp(-f)); % Prandtl correction factor
cl=interp1(Aoa,Cl,aoa,'linear','extrap');
cd=interp1(Aoa,Cd,aoa,'linear','extrap');
Cn = cl*cosd(Phi)+cd*sind(Phi);
Ct =cl*sind(Phi)-cd*cosd(Phi);
K=(4*F*(sind(Phi))^2)/(sigma(i)*Cn);
%%%%%%%%%% a and ac condition
a = 1/(((4*F*(sind(Phi)^2))/(sigma(i)*Cn))+1);
if a < ac || a==ac
a=a;
else
a = 0.5*(2+K*(1-2*ac)-sqrt(((K*(1-2*ac)+2)^2)+4*((K*(ac^2))-1)));
end
a_prime =1/(((4*F*(sind(Phi)*cosd(Phi)))/(sigma(i)*Ct))-1);
alist(i,n+1)=a;
alistprime(i,n+1)=a_prime;
%%% condition for epsilon or tolerance
if a-alist(i,n) < eps && a_prime-alistprime(i,n) < eps
break;
end
end
%%% V relative
v_rel(i) = sqrt( (Velocity(j)*(1-alist(end)))^2 + (w*r(i)*(1+alistprime(end)))^2 );
%%% The tangential force per length Pt
Pt(i) = 0.5*Rho*(v_rel(i)^2)*Chord(i)*Ct;
end
for i=8:length(Pt)-1
Ai(i) = (Pt(i+1)-Pt(i) ) / (r(i+1)-r(i));
Bi(i) = ((Pt(i)*r(i+1))-(Pt(i+1)*r(i)) ) / (r(i+1)-r(i));
Pt_total(i) = Ai(i)*r(i)+Bi(i);
end
for i=8:length(Pt)-1
M(i) = ((1/3)*Ai(i)*((r(i+1)^3)-(r(i)^3))) + ((0.5)*Bi(i)*((r(i+1)^2)-(r(i)^2)));
end
% % total shaft torque and power
MTotal(j) = B* sum(M);
Power(j)=MTotal(j)*w*1e-3;
if k==3
TorqueBaseline=MTotal;
end
end
if k==3
figure(1)
hold on
leg = sprintf(' Baseline ');
plot(Velocity,MTotal,'-oblack','LineWidth',1.5, 'displayname', leg); grid on
end
if k>3
figure(1)
hold on
leg = sprintf('config %g',nc(iter)-1);
plot(Velocity,MTotal, 'displayname', leg); grid on
xlabel(' Wind Speed (m/s) '); ylabel(' Torque (Nm) ');
set(gca,'XTick',(4:1:20)); set(gca,'YTick',(0:300:1800))
ylim([0 1800]); xlim([4 20]);
end
if k==33
figure(1)
hold on
leg = sprintf('Experimental upper range ');
plot(vupper,torqueupper,'-or','LineWidth',1.5, 'displayname', leg)
figure(1)
hold on
leg = sprintf('Experimental lower range ');
plot(vlower,torquelower,'-or','LineWidth',1.5,'displayname', leg)
end
for i =1
if Aoa(i)>-19
title(' Reynolds number = 500.000' )
else
title(' Reynolds number = 1.000.000' )
end
end
end
legend show

Cris LaPierre on 1 Jan 2022
Markers in your legend come from the linespec used when plotting your line. Plot each line with a different marker style. MATLAB has 15 markers to pick from.
Somewhere in your code before the for loop define the markers:
mrkr = 'o+*.x_|sd^v><ph';
and then when you plot, add a marker style to your code
plot(Velocity,MTotal,"Marker",mrkr(nc(iter)-1),'displayname', leg);
This will only work if you have <=15 lines to plot.
Cris LaPierre on 1 Jan 2022
Did you leave off the code for the legend?
%%% Fixed Parameters
B = 2; % Number of Blades
Rho = 1.225; % Density of air kg/m3
w = 7.501; % Rotational Speed rad/s
Theta_P = -3; % Tip Pitch Angle in degree
ac = 0.2; % Glauert correction
eps = 1e-5; % Error tolerance
mrkr = 'o+*.x_|sd^v><ph';
nc=1:1:16; % number of configurations for effect off different distribution
%%% Airfoil Date
Aoa = afoil(:,1);
Cl = afoil(:,2);
Cd = afoil(:,3);
Velocity =[5,6,7,8,9,11]; % Wind Speed m/s
MTotal=[];
%%% Torque vs Velocity Data
vupper=torquee(1:5,1); % Radial Distance in meter
vlower=torquee(6:10,1); % Chord length in meter
torquelower=torquee(1:5,2); % Twist in degree
torqueupper=torquee(6:10,2);
sections = readmatrix('chord and twist 15 distributions.xlsx');
iter=0;
for k=3:2:33
iter=iter+1; % used in legends in plots
r=sections(1:26,2); % Radial Distance in meter
SS=sections(1:26,3); % Span Station %
Chord=sections(1:26,k+1); % Chord length in meter
Beta=sections(1:26,k+2); % Twist in degree
for j=1:length(Velocity)
for i = 8:length(r)
sigma(i) = (B*Chord(i))/(2*pi*r(i));
a=0;
a_prime=0;
alist(i)=[0];
alistprime(i)=[0];
for n=1:1000
Phi = atand(((1-a)*Velocity(j))/((1+a_prime)*w*r(i))); % Flow Angle
Theta = Beta(i) + Theta_P ;
aoa = Phi- Theta; % local angle of attack in degree
f = (B/2)* ((R-r(i))/(r(i)*sind(Phi)));
F = (2/pi)*acos(exp(-f)); % Prandtl correction factor
cl=interp1(Aoa,Cl,aoa,'linear','extrap');
cd=interp1(Aoa,Cd,aoa,'linear','extrap');
Cn = cl*cosd(Phi)+cd*sind(Phi);
Ct =cl*sind(Phi)-cd*cosd(Phi);
K=(4*F*(sind(Phi))^2)/(sigma(i)*Cn);
%%%%%%%%%% a and ac condition
a = 1/(((4*F*(sind(Phi)^2))/(sigma(i)*Cn))+1);
if a < ac || a==ac
a=a;
else
a = 0.5*(2+K*(1-2*ac)-sqrt(((K*(1-2*ac)+2)^2)+4*((K*(ac^2))-1)));
end
a_prime =1/(((4*F*(sind(Phi)*cosd(Phi)))/(sigma(i)*Ct))-1);
alist(i,n+1)=a;
alistprime(i,n+1)=a_prime;
%%% condition for epsilon or tolerance
if a-alist(i,n) < eps && a_prime-alistprime(i,n) < eps
break;
end
end
%%% V relative
v_rel(i) = sqrt( (Velocity(j)*(1-alist(end)))^2 + (w*r(i)*(1+alistprime(end)))^2 );
%%% The tangential force per length Pt
Pt(i) = 0.5*Rho*(v_rel(i)^2)*Chord(i)*Ct;
end
for i=8:length(Pt)-1
Ai(i) = (Pt(i+1)-Pt(i) ) / (r(i+1)-r(i));
Bi(i) = ((Pt(i)*r(i+1))-(Pt(i+1)*r(i)) ) / (r(i+1)-r(i));
Pt_total(i) = Ai(i)*r(i)+Bi(i);
end
for i=8:length(Pt)-1
M(i) = ((1/3)*Ai(i)*((r(i+1)^3)-(r(i)^3))) + ((0.5)*Bi(i)*((r(i+1)^2)-(r(i)^2)));
end
% % total shaft torque and power
MTotal(j) = B* sum(M);
Power(j)=MTotal(j)*w*1e-3;
if k==3
TorqueBaseline=MTotal;
end
end
if k==3
figure(1)
hold on
leg = sprintf(' Baseline ');
plot(Velocity,MTotal,'-oblack','LineWidth',1.5, 'displayname', leg); grid on
end
if k>3
figure(1)
hold on
leg = sprintf('config %g',nc(iter)-1);
plot(Velocity,MTotal,"Marker",mrkr(nc(iter)-1),'displayname', leg); grid on
xlabel(' Wind Speed (m/s) '); ylabel(' Torque (Nm) ');
set(gca,'XTick',(4:1:20)); set(gca,'YTick',(0:300:1800))
ylim([0 1800]); xlim([4 20]);
end
if k==33
figure(1)
hold on
leg = sprintf('Experimental upper range ');
plot(vupper,torqueupper,'-or','LineWidth',1.5, 'displayname', leg)
figure(1)
hold on
leg = sprintf('Experimental lower range ');
plot(vlower,torquelower,'-or','LineWidth',1.5,'displayname', leg)
end
for i =1
if Aoa(i)>-19
title(' Reynolds number = 500.000' )
else
title(' Reynolds number = 1.000.000' )
end
end
end
legend show

_ on 1 Jan 2022
close all
clear all
clc
%%% Fixed Parameters
B = 2; % Number of Blades
Rho = 1.225; % Density of air kg/m3
w = 7.501; % Rotational Speed rad/s
Theta_P = -3; % Tip Pitch Angle in degree
ac = 0.2; % Glauert correction
eps = 1e-5; % Error tolerance
nc=1:1:16; % number of configurations for effect off different distribution
%%% Airfoil Date
Aoa = afoil(:,1);
Cl = afoil(:,2);
Cd = afoil(:,3);
Velocity =[5,6,7,8,9,11]; % Wind Speed m/s
MTotal=[];
%%% Torque vs Velocity Data
vupper=torquee(1:5,1); % Radial Distance in meter
vlower=torquee(6:10,1); % Chord length in meter
torquelower=torquee(1:5,2); % Twist in degree
torqueupper=torquee(6:10,2);
sections = readmatrix('chord and twist 15 distributions.xlsx');
iter=0;
n_lines = (33-3)/2+1;
all_markers = '+*.xsd^v><pho';
all_markers = repmat(all_markers,1,ceil(n_lines/numel(all_markers)));
my_markers = repmat(' ',1,33);
my_markers(3:2:33) = all_markers(1:n_lines);
for k=3:2:33
iter=iter+1; % used in legends in plots
r=sections(1:26,2); % Radial Distance in meter
SS=sections(1:26,3); % Span Station %
Chord=sections(1:26,k+1); % Chord length in meter
Beta=sections(1:26,k+2); % Twist in degree
for j=1:length(Velocity)
for i = 8:length(r)
sigma(i) = (B*Chord(i))/(2*pi*r(i));
a=0;
a_prime=0;
alist(i)=[0];
alistprime(i)=[0];
for n=1:1000
Phi = atand(((1-a)*Velocity(j))/((1+a_prime)*w*r(i))); % Flow Angle
Theta = Beta(i) + Theta_P ;
aoa = Phi- Theta; % local angle of attack in degree
f = (B/2)* ((R-r(i))/(r(i)*sind(Phi)));
F = (2/pi)*acos(exp(-f)); % Prandtl correction factor
cl=interp1(Aoa,Cl,aoa,'linear','extrap');
cd=interp1(Aoa,Cd,aoa,'linear','extrap');
Cn = cl*cosd(Phi)+cd*sind(Phi);
Ct =cl*sind(Phi)-cd*cosd(Phi);
K=(4*F*(sind(Phi))^2)/(sigma(i)*Cn);
%%%%%%%%%% a and ac condition
a = 1/(((4*F*(sind(Phi)^2))/(sigma(i)*Cn))+1);
if a < ac || a==ac
a=a;
else
a = 0.5*(2+K*(1-2*ac)-sqrt(((K*(1-2*ac)+2)^2)+4*((K*(ac^2))-1)));
end
a_prime =1/(((4*F*(sind(Phi)*cosd(Phi)))/(sigma(i)*Ct))-1);
alist(i,n+1)=a;
alistprime(i,n+1)=a_prime;
%%% condition for epsilon or tolerance
if a-alist(i,n) < eps && a_prime-alistprime(i,n) < eps
break;
end
end
%%% V relative
v_rel(i) = sqrt( (Velocity(j)*(1-alist(end)))^2 + (w*r(i)*(1+alistprime(end)))^2 );
%%% The tangential force per length Pt
Pt(i) = 0.5*Rho*(v_rel(i)^2)*Chord(i)*Ct;
end
for i=8:length(Pt)-1
Ai(i) = (Pt(i+1)-Pt(i) ) / (r(i+1)-r(i));
Bi(i) = ((Pt(i)*r(i+1))-(Pt(i+1)*r(i)) ) / (r(i+1)-r(i));
Pt_total(i) = Ai(i)*r(i)+Bi(i);
end
for i=8:length(Pt)-1
M(i) = ((1/3)*Ai(i)*((r(i+1)^3)-(r(i)^3))) + ((0.5)*Bi(i)*((r(i+1)^2)-(r(i)^2)));
end
% % total shaft torque and power
MTotal(j) = B* sum(M);
Power(j)=MTotal(j)*w*1e-3;
if k==3
TorqueBaseline=MTotal;
end
end
if k==3
figure(1)
hold on
leg = sprintf(' Baseline ');
plot(Velocity,MTotal,'-oblack','LineWidth',1.5, 'displayname', leg); grid on
end
if k>3
figure(1)
hold on
leg = sprintf('config %g',nc(iter)-1);
% plot(Velocity,MTotal, 'displayname', leg); grid on
plot(Velocity,MTotal,[my_markers(k) '-'], 'displayname', leg); grid on
xlabel(' Wind Speed (m/s) '); ylabel(' Torque (Nm) ');
set(gca,'XTick',(4:1:20)); set(gca,'YTick',(0:300:1800))
ylim([0 1800]); xlim([4 20]);
end
if k==33
figure(1)
hold on
leg = sprintf('Experimental upper range ');
plot(vupper,torqueupper,'-or','LineWidth',1.5, 'displayname', leg)
figure(1)
hold on
leg = sprintf('Experimental lower range ');
plot(vlower,torquelower,'-or','LineWidth',1.5,'displayname', leg)
end
for i =1
if Aoa(i)>-19
title(' Reynolds number = 500.000' )
else
title(' Reynolds number = 1.000.000' )
end
end
end
legend show
mehmet salihi on 1 Jan 2022

Sulaymon Eshkabilov on 1 Jan 2022
Edited: Cris LaPierre on 1 Jan 2022
There are a few different ways to do that. But before jumping to the plot options, in your code some of the loops can be eliminated to enahce the computation time, e.g.:
This loop:
for i=8:length(Pt)-1
Ai(i) = (Pt(i+1)-Pt(i) ) / (r(i+1)-r(i));
Bi(i) = ((Pt(i)*r(i+1))-(Pt(i+1)*r(i)) ) / (r(i+1)-r(i));
Pt_total(i) = Ai(i)*r(i)+Bi(i);
end
for i=8:length(Pt)-1
M(i) = ((1/3)*Ai(i)*((r(i+1)^3)-(r(i)^3))) + ((0.5)*Bi(i)*((r(i+1)^2)-(r(i)^2)));
end
Can be substituted with:
N = length(Pt);
Ai = diff(Pt(8:N))./(diff(r(8:N).'));
Bi = ((Pt(8:N-1).*(r(9:N).'))-(Pt(9:N).*(r(8:N-1).')))./(diff(r(8:N).'));
Pt_total = Ai.*(r(8:N-1).')+Bi;
M = ((1/3)*Ai.*((r(9:N).^3).'-(r(8:N-1).^3).')) + ((0.5)*Bi.*((r(9:N).^2).'-(r(8:N-1).^2).'));
Now, how to get nice looking legends and here is the complete code:
close all; clearvars; clc
%%% Fixed Parameters
B = 2; % Number of Blades
Rho = 1.225; % Density of air kg/m3
w = 7.501; % Rotational Speed rad/s
Theta_P = -3; % Tip Pitch Angle in degree
ac = 0.2; % Glauert correction
eps = 1e-5; % Error tolerance
nc=1:1:16; % number of configurations for effect off different distribution
%%% Airfoil Date
Aoa = afoil(:,1);
Cl = afoil(:,2);
Cd = afoil(:,3);
Velocity =[5,6,7,8,9,11]; % Wind Speed m/s
MTotal=[];
%%% Torque vs Velocity Data
vupper=torquee(1:5,1); % Radial Distance in meter
vlower=torquee(6:10,1); % Chord length in meter
torquelower=torquee(1:5,2); % Twist in degree
torqueupper=torquee(6:10,2);
sections = readmatrix('chord and twist 15 distributions.xlsx');
iter=0;
for k=3:2:33
iter=iter+1; % used in legends in plots
r=sections(1:26,2); % Radial Distance in meter
SS=sections(1:26,3); % Span Station %
Chord=sections(1:26,k+1); % Chord length in meter
Beta=sections(1:26,k+2); % Twist in degree
for j=1:length(Velocity)
for i = 8:length(r)
sigma(i) = (B*Chord(i))/(2*pi*r(i));
a=0;
a_prime=0;
alist(i)=[0];
alistprime(i)=[0];
for n=1:1000
Phi = atand(((1-a)*Velocity(j))/((1+a_prime)*w*r(i))); % Flow Angle
Theta = Beta(i) + Theta_P ;
aoa = Phi- Theta; % local angle of attack in degree
f = (B/2)* ((R-r(i))/(r(i)*sind(Phi)));
F = (2/pi)*acos(exp(-f)); % Prandtl correction factor
cl=interp1(Aoa,Cl,aoa,'linear','extrap');
cd=interp1(Aoa,Cd,aoa,'linear','extrap');
Cn = cl*cosd(Phi)+cd*sind(Phi);
Ct =cl*sind(Phi)-cd*cosd(Phi);
K=(4*F*(sind(Phi))^2)/(sigma(i)*Cn);
%%%%%%%%%% a and ac condition
a = 1/(((4*F*(sind(Phi)^2))/(sigma(i)*Cn))+1);
if a < ac || a==ac
a=a;
else
a = 0.5*(2+K*(1-2*ac)-sqrt(((K*(1-2*ac)+2)^2)+4*((K*(ac^2))-1)));
end
a_prime =1/(((4*F*(sind(Phi)*cosd(Phi)))/(sigma(i)*Ct))-1);
alist(i,n+1)=a;
alistprime(i,n+1)=a_prime;
%%% condition for epsilon or tolerance
if a-alist(i,n) < eps && a_prime-alistprime(i,n) < eps
break;
end
end
%%% V relative
v_rel(i) = sqrt( (Velocity(j)*(1-alist(end)))^2 + (w*r(i)*(1+alistprime(end)))^2 );
%%% The tangential force per length Pt
Pt(i) = 0.5*Rho*(v_rel(i)^2)*Chord(i)*Ct;
end
N = length(Pt);
Ai = diff(Pt(8:N))./(diff(r(8:N).'));
Bi = ((Pt(8:N-1).*(r(9:N).'))-(Pt(9:N).*(r(8:N-1).')))./(diff(r(8:N).'));
Pt_total = Ai.*(r(8:N-1).')+Bi;
M = ((1/3)*Ai.*((r(9:N).^3).'-(r(8:N-1).^3).')) + ((0.5)*Bi.*((r(9:N).^2).'-(r(8:N-1).^2).'));
% % total shaft torque and power
MTotal(j) = B* sum(M);
Power(j)=MTotal(j)*w*1e-3;
if k==3
TorqueBaseline=MTotal;
end
end
if k==3
figure(1)
hold on
leg = sprintf(' Baseline ');
plot(Velocity,MTotal,'-oblack','LineWidth',1.5, 'displayname', leg); grid on
end
CC = 'rgbmkcgbgmcmrgbkm'; % Color Selection for lines and markerfaces
LL = '-:--'; % Line Type
MM = 'odphs<>^'; % Marker Type
if k>3
leg = sprintf('config %g',nc(iter)-1);
Ls=randi(numel(LL),1); % Random selection of Line Type
Ms=randi(numel(MM),1); % Random selection of Marker Type
CLs=randi(numel(CC),1); % Random selection of Line Color
CMs=randi(numel(CC),1); % Random selection of Marker Face Color
LMC =[CC(CLs) LL(Ls) MM(Ms)];
plot(Velocity,MTotal, LMC, 'markerfacecolor', CC(CMs), 'displayname', leg); grid on
xlabel(' Wind Speed (m/s) '); ylabel(' Torque (Nm) ');
set(gca,'XTick',(4:1:20)); set(gca,'YTick',(0:300:1800))
ylim([0 1800]); xlim([4 20]);
end
if k==33
leg = sprintf('Experimental upper range ');
plot(vupper,torqueupper,'-or','LineWidth',1.5, 'displayname', leg)
leg = sprintf('Experimental lower range ');
plot(vlower,torquelower,'-or','LineWidth',1.5,'displayname', leg)
end
for i =1
if Aoa(i)>-19
title(' Reynolds number = 500.000' )
else
title(' Reynolds number = 1.000.000' )
end
end
end
legend show
Sulaymon Eshkabilov on 2 Jan 2022
Most welcome! Glad to be of some help.