MATLAB: How to use the legend command to display the label of the samples in a plot

image analysisimage processinglegend

I have written a 2 functions, in which each displays a plot graph at the end…
now i want to display both graph in same plot…. by calling each function one after other and entering "hold all" , i'm getting the graph i wanted…. now the problem is the legend information is being displayed only the last function graph…. can anyone solve this problem

Best Answer

Hi,
you have to call legend once so that it displays the information of both functions. So for now you do roughly that
plot(1:10);
legend('a')
hold all
plot(1:10,sin(1:10))
legend('b')
hold off
But you need to call it like this
plot(1:10);
hold all
plot(1:10,sin(1:10))
legend('a','b')
hold off
Or in the case you don't know the legend tags you can use the existing legend and recreate a new one using the old legend tags
plot(1:10);
legend('a')
hold all
plot(1:10,sin(1:10))
legend([get(legend,'String'),{'b'}])
hold off