title: Matlab: Calling a legend in a plot which is created with an ''if'' statement
body: <p>I am trying to plot some functions and display a legend. My code can be found below:</p>
  
  <pre><code>%% DATCOM spanloading method
  tol = Input.tol; 
  iteration = 0;
  difference =0;
  AVL_step = 0.25;
  Interp_step =0.1;
  AVLruns = 3;
  Angle = ((1:AVLruns)*AVL_step)+AOA;
  
  
   while sum(difference &lt; 0) &lt;= fix(tol*Input.Surface.Nspan) &amp;&amp; iteration &lt; 100
  
  iteration = iteration +1;
  
  
                            if iteration &lt;= AVLruns
                                      AOA =  AOA + AVL_step;
                                      [Yle_wing,Spanloading] = obj.AVLspanloading(Input,CLa,AOA);                % Creates spanloading with AVL
                                      Scalefunc = 1/(max(Yle_wing)-min(Yle_wing));                               % Scale function
                                      Ynorm= ((Yle_wing - min(Yle_wing)) .* Scalefunc)';                         % Normalize semi-span from 0 to 1
  
                                          if length(YClmax) ~= length(Ynorm) &amp;&amp; iteration ==1                                               
                                              Clmax_dist= interp1(YClmax,Clmax_dist,Ynorm,'linear');                   
                                          end 
  
                              difference = (Clmax_dist - Spanloading);                                           % Difference between resampled  CL3d and Cl2d  
                              cl_matrix(iteration,:) = Spanloading;
                            else  
                              AOA =  AOA + Interp_step;
                                      for QQ = 1:Input.Surface.Nspan 
                                          CL3d = interp1(Angle,cl_matrix(:,QQ)',AOA,'linear','extrap');
                                          Spanloading(:,QQ) = CL3d;
                                      end
                              difference = (Clmax_dist - Spanloading); 
                            end
  
  
  
    figure(1)
    pl = plot(Yle_wing,Clmax_dist,'r');
    legendStrs = {'2D Clmax'};
    set(pl,'linewidth',1.5);
    hold on 
    if iteration &lt;= AVLruns
      plot(Yle_wing,Spanloading,'g--o')
      legendStrs = [legendStrs, {'Spanloading by AVL'}];
    else
      plot(Yle_wing,Spanloading)
      legendStrs = [legendStrs, {'Spanloading by extrapolation'}];
    end
    xlabel('2y/b')
    ylabel('Local Cl') 
    title('DATCOM SPANLOADING METHOD')
    legend('boxon')
    legend(legendStrs,'Location','SouthWest');
  
  
  
   end
   if iteration &gt;= 100
       disp('Spanloading did not converge, while loop terminated by reaching maximum iteration count')    
   end
  </code></pre>
  
  <p>Here, I create a plot in the same figure using the <code>hold on</code> statement and with an if statement. Running my code will run both conditions of the if statement. Hence, multiple lines will be plotted in this Figure.</p>
  
  <p>Therefore, I want to make a legend for all three plot commands. However, I don't seem to understand how to create the legend for a plot function within an if statement since the following makes my second plot green, and the rest of the plots red.</p>
  
  <p>How would I go about?</p>
  
  <p>Edit: I've incorporated my whole <code>while</code> loop</p>