Plot Legend Not Correct
显示 更早的评论
So I have a code and want to plot a couple of features. Long story short, my plot legend is a little confused as to its labels. How can I correct this?
clear;clc;
%inputs
d=5; %feet
h=10; %feet
yi=7; %feet
ysp=5; %feet
dt=30; %seconds
band=1; %feet
%givens
Vimax=100; %gallons per minute
Vimax=Vimax*0.002228; %ft^3/s
aTank=(pi*d^2)/4; %ft^2
tmax=3000;
uL=ysp+band/2;
lL=ysp-band/2;
%valve position
if yi>uL
vp=0;
elseif yi<lL
vp=1;
else
vp=0;
end
t=0;
y=yi;
n=1;
while t(n)<=tmax
%net flow rate
nfl=Vimax*vp-Vout(t); %ft^3/s
%net volume change for a time step
vc=nfl*dt; %ft^3
%determine change in water level
dy=vc/aTank; %feet
y(n+1)=y(n)+dy;
t(n+1)=t(n)+dt;
%next
if y(n+1)>uL
vp=0;
elseif y(n+1)<lL
vp=1;
else
vp=vp;
%vp=-(y(n+1)-ysp)/band+0.5; %CHANGE TO PROPORTIONAL
end
n=n+1;
end
plot(t,y,'r')
hold on
plot(t,ysp,'g+')
plot(t,uL,'bo')
plot(t,lL,'y.')
xlabel('Time in Seconds')
ylabel('Water Level in Feet')
legend('Water Level','Setpoint','Upper Level','Lower Level')

采纳的回答
更多回答(1 个)
Rik
2018-6-27
You can use the output of the plot function to get the handles to the objects. Then you can use the list as an input to legend.
temp=plot(t,y,'r');
plothandles=temp(1);
hold on
temp=plot(t,ysp,'g+');
plothandles(2)=temp(1);
temp=plot(t,uL,'bo');
plothandles(3)=temp(1);
temp=plot(t,lL,'y.');
plothandles(4)=temp(1);
xlabel('Time in Seconds')
ylabel('Water Level in Feet')
legend(plothandles,{'Water Level','Setpoint','Upper Level','Lower Level'})
类别
在 帮助中心 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!