setting two legend for two function

4 visualizzazioni (ultimi 30 giorni)
Moslem Uddin
Moslem Uddin il 9 Gen 2019
Modificato: Luna il 12 Gen 2019
Please help me in setting two legend for two functions in the attached code.

Risposta accettata

Luna
Luna il 9 Gen 2019
Modificato: Luna il 9 Gen 2019
Moslem,
You can try this below, it is a little bit tricky to add legend to axis handle as parent. Then add other legend.
See my comments in green below:
syms y
p=.965; m1=.28;b=[2 5 7];c=0.7;
q=.585e-3; m2=.0125; x=pi/4; h1=10; h2=5;
g1=p*sin(x);g2=q*sin(x);a1=(g1.*h1+g2.*h2)/h1;
%for i=1:length(b)
%y = 0:0.5:10;
hfig = figure;
for i=1:length(b)
f=y.*(g1/(2*m1)).*(2.*h1-y)+(g2*h2/m1).*y+b(i);
hPlotf(i)= fplot(f,y,[0,h1]); % hPlot is handle of function plot f
hold on;
%y = 10:0.5:15;
f1=y.*(g2/(2*m2)).*(2.*(h1+h2)-y)+((g1/(2*m1))-(g2/(2*m2))).*(h1^2)+(g2.*h1.*h2).*((1/m1)-(1/m2))+b(i);
%feval(f1,y)
%figure
hPlotf1(i) = fplot(f1,y,[h1,15]); % hPlot is handle of function plot f1
hold on;
end
a=axes('position',get(gca,'position'),'visible','off'); % a is the current axis handle
hLf = legend(a,hPlotf,'b = 2','b = 5','b=7','Location','SouthWest'); %hLf - legend handle for f
text(a, hLf.Position(1)-0.1,hLf.Position(2)+0.02,'f'); % adding text for f according to hLf position
hLf1 = legend(hPlotf1,'b = 2','b = 5','b=7','Location','NorthWest'); %hLf1 - legend handle for f1
text(a, hLf1.Position(1)-0.1,hLf1.Position(2)+0.02,'f1'); % adding text for f1 according to hLf1 position
xlabel('y','FontSize', 20);
ylabel('u_2','FontSize', 20);
%end
  3 Commenti
Luna
Luna il 11 Gen 2019
Which version of Matlab you are using?
Luna
Luna il 11 Gen 2019
Modificato: Luna il 12 Gen 2019
OK now I understand it is because the axis we are trying to put labels is not currently focused axis handle object.
Move xlabel and ylabel lines right below the end of the for loop as follows, then it will fix your problem.
hPlotf1(i) = fplot(f1,y,[h1,15]); % hPlot is handle of function plot f1
hold on;
end
xlabel('y','FontSize', 20);
ylabel('u_2','FontSize', 20);
Please accept answer if it is working :)

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by