Balraj Boyal
Balraj Boyal

Reputation: 155

Matlab: Calling a legend in a plot which is created with an ''if'' statement

I am trying to plot some functions and display a legend. My code can be found below:

%% 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 < 0) <= fix(tol*Input.Surface.Nspan) && iteration < 100

iteration = iteration +1;


                          if iteration <= 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) && 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 <= 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 >= 100
     disp('Spanloading did not converge, while loop terminated by reaching maximum iteration count')    
 end

Here, I create a plot in the same figure using the hold on 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.

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.

How would I go about?

Edit: I've incorporated my whole while loop

Upvotes: 2

Views: 676

Answers (1)

Some Guy
Some Guy

Reputation: 1797

You can pass a cell array as an input to legend. Maintain a cell array of strings and add relevant strings to it right after your plot statements.

pl = plot(Yle_wing,Clmax_dist,'r');
legendStrs = {'2D Clmax'};

Then later in the if else block

if iteration <= AVLruns
    plot(Yle_wing,Spanloading,'g--o')
    legendStrs = [legendStrs, {'Spanloading by AVL'}];
    set(pl,'linewidth',1.5);
else
    plot(Yle_wing,Spanloading)
    legendStrs = [legendStrs, {'Spanloading by extrapolation'}];
end

This will keep the number of legend strings equal to the number of lines you have on your plot. Then, at last

legend(legendStrs,'Location','SouthWest');

Upvotes: 1

Related Questions