MATLAB: How to create multiple legends – depending on number of inputs

legendMATLABplot

So I basically have this function y which varies on a chosen number nabla. I have written a function that takes in a chosen number of nablas and plots the corresponding y value to a given x interval, here it is:
if true
function nablaplot(n)
%writing in the number n of inputs and creating a matrix
%calculating my matrix y which I will plot
for i=1:n
nabla(i)=input(sprintf('Enter nabla value number %i: ',i));
y(i,:) = 2.*x - 2.*x.^3 + x.^4 + (nabla(i)/6).*x.*(1-x).^3 ;
end
x=0:0.01:1; %step length
figure
y1= 0*x; %x-axis

plot(x,y1,':black') %x-axis
hold on
plot(x,y) %here I plot the number n of graphs
axis([0 1 -0.2 2]) %boundaries
hold off
end
end
This function prints the given number n of graphs into a single plot – which is what I want! But is there any way to plot n legends which states what nabla value each graph has?

Best Answer

for example:
figure;
hold on;
x=1:10
for i=1:3
plot(i*sin(x));
nabla(i)=i;
end
leg=string(nabla); %converts integer-array to string array
legend(leg);
So your values for nabla have to be in a string array and in the right order, which is the input to the legend(...) function.
Related Question