matlab draws a diagram using the stem function and adds other elements to the legend

3 views (last 30 days)
I want to use the stem function to draw my results, but when I use it to add a legend to my graph, there are 3 extra sets of data on the legend that I don't want. How do I change this?
I just want to keep FOV=90,data1 and data5.
% room dimension
rx = 4;
ry = 8;
rz = 3;
% Lambertian emission order
n = 1;
ne = 1;
% detector physical area of a PD
A = 1e-4;
% FOV of a receiver
FOV = 90;
% speed of light
c = 3 * (10^8);
% %the gain of an optical concentrator;
index = 1.5;
% total average transmitted optical power
%Responsivity for red, yellow, green, and blue 0.8 W, 0.5 W, 0.3 W, and 0.3 W
Ps = 36;
% Size of reflection area
dA1 = 0.05 * 0.05;
dA2 = 0.2 * 0.2;
% Set sampling time
timebin = 1e-8;
% reflection coefficients of wall
Rho1 = 0.8;
Rho2 = 0.8;
%% first transmitter calculation
RP = [1 2 1];
% Distance between transmitter and receiver
% reciver
y=linspace(1,7,4);
x=[1 3];
% Divide the wall into areas according to the reflected area(1st reflection)
Nx1 = rx * 20;
Ny1 = ry * 20;
Nz1 = rz * 20;
x1=linspace(-rx/2,rx/2,Nx1);
y1=linspace(-ry/2,ry/2,Ny1);
z1=linspace(-rz/2,rz/2,Nz1);
% Divide the wall into areas according to the reflected area(2nd reflection)
Nx2 = rx * 5;
Ny2 = ry * 5;
Nz2 = rz * 5;
x2=linspace(-rx/2,rx/2,Nx2);
y2=linspace(-ry/2,ry/2,Ny2);
z2=linspace(-rz/2,rz/2,Nz2);
% Used to store impulse response
%% For the LOS channel, the DC gain
% receiver position vector
for ii = 1:2
for jj = 1:4
TP1=[x(ii) y(jj) 3]; % receiver position vector
h_a1(ii,jj)=0; % reflection from North face
DLOS = sqrt(dot(TP1-RP,TP1-RP));
cos_alphaLOS = abs(RP(3)-TP1(3))/DLOS;
cos_betaLOS = abs(TP1(3)-RP(3))/DLOS;
t(ii,jj)= DLOS/c;
if abs(acosd(cos_betaLOS)) <= 90
h_a1(ii,jj) =h_a1(ii,jj) + ((n+1) * A * ((abs(RP(3)-TP1(3))/DLOS) ^ n) * (abs(TP1(3)-RP(3))) * Ps)...
/ (2 * pi * ((sqrt(dot(TP1-RP,TP1-RP))) ^ 2));
end
end
end
for ii = 1:2
for jj = 1:4
TP1=[x(ii) y(jj) 3]; % receiver position vector
h_a2(ii,jj)=0; % reflection from North face
DLOS = sqrt(dot(TP1-RP,TP1-RP));
cos_alphaLOS = abs(RP(3)-TP1(3))/DLOS;
cos_betaLOS = abs(TP1(3)-RP(3))/DLOS;
t2(ii,jj)= DLOS/c;
if abs(acosd(cos_betaLOS)) <= 60
h_a2(ii,jj) = h_a2(ii,jj) + ((n+1) * A * ((abs(RP(3)-TP1(3))/DLOS) ^ n) * (abs(TP1(3)-RP(3))) * Ps)...
/ (2 * pi * ((sqrt(dot(TP1-RP,TP1-RP))) ^ 2));
end
end
end
for ii = 1:2
for jj = 1:4
TP1=[x(ii) y(jj) 3]; % receiver position vector
h_a3(ii,jj)=0; % reflection from North face
DLOS = sqrt(dot(TP1-RP,TP1-RP));
cos_alphaLOS = abs(RP(3)-TP1(3))/DLOS;
cos_betaLOS = abs(TP1(3)-RP(3))/DLOS;
t3(ii,jj)= DLOS/c;
if abs(acosd(cos_betaLOS)) <= 30
h_a3(ii,jj) = h_a3(ii,jj) + ((n+1) * A * ((abs(RP(3)-TP1(3))/DLOS) ^ n) * (abs(TP1(3)-RP(3))) * Ps)...
/ (2 * pi * ((sqrt(dot(TP1-RP,TP1-RP))) ^ 2));
end
end
end
x1=t/timebin;
stem(x1,h_a1,'black','Marker','none','DisplayName','FOV=90');
xlim([0 3])
title('The impluse response of Los channel')
xlabel('Time[ns]')
ylabel('Power[w]')
hold on;
stem((t2/timebin),h_a2,':','red','Marker','none');
hold on;
stem((t3/timebin),h_a3,'--','green','Marker','none');

Accepted Answer

Dave B
Dave B on 2 Nov 2021
Edited: Dave B on 2 Nov 2021
It looks to me like the inputs you passed to stem have four columns, so you've made 12 Stem objects in your axes. I'm not sure if this was your intention? Assuming so, the best strategy is tell legend explicitly which objects to include. Below I do that with some random data and just use the first stem object from each call (as they all the same display characteristics):
s1=stem(1:10,rand(10,4),'black','Marker','none','DisplayName','FOV=90');
hold on;
s2=stem(1:10,rand(10,4),':','red','Marker','none');
s3=stem(1:10,rand(10,4),'--','green','Marker','none');
legend([s1(1) s2(1) s3(1)])
Note that you'll still see the getcolumn bit appended to the DisplayName, to fix this, just specify labels in the call to legend:
figure
s1=stem(1:10,rand(10,4),'black','Marker','none','DisplayName','FOV=90');
hold on;
s2=stem(1:10,rand(10,4),':','red','Marker','none');
s3=stem(1:10,rand(10,4),'--','green','Marker','none');
legend([s1(1) s2(1) s3(1)],["FOV=90" "Other" "Something"])

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!