function plotlegend
%
%   Name: plotlegend
%
%   Usage: plotlegend
%
%   Description: Plots the legend for the DISPLAY data vectors
%     found in the axes 'iaxes' and the data set 'inds' in
%     the legend position 'ipos'
%     
%   Global Variables:
%      DISPLAY
%      DATA
%
%
%   Version SWD970804
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This matlab source code was originally     %
% developed as part of "DIAMOND" at          %
% Los Alamos National Laboratory. It may     %
% be copied, modified, and distributed in    %
% any form, provided:                        %
%  a) This notice accompanies the files and  %
%     appears near the top of all source     %
%     code files.                            %
%  b) No payment or commercial services are  %
%     received in exchange for the code.     %
%                                            %
% Original copyright is reserved by the      %
% Regents of the University of California,   %
% in addition to Scott W. Doebling, Phillip  %
% J. Cornwell, Erik G. Straser, and Charles  %
% R. Farrar.                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%   Access required Global Variables

global DISPLAY DATA iaxesvec indsvec

%   Extract the handle of the legend axis

hlegax = DISPLAY{1}.hlegax;  % There is only 1 legend axis allowed

if ishandle(hlegax),
   
   %   Define spacing properties for legend text

   top = 1.1;
   spc = 0.1;

   axes(hlegax)

   %   Delete existing legend items

   dellegend
   
   naxes = length(iaxesvec);
   
   %   Loop over display axes and display lines

   for iaxtemp = iaxesvec,
   
      for indstemp = indsvec,
         
         ipos = (iaxtemp-1)*naxes + (indstemp-1);
         
         %     Construct Text

         idatafile = DISPLAY{iaxtemp}.idatafile{indstemp};

         legtxt = [DISPLAY{iaxtemp}.lnlab{indstemp},', File=',DATA{idatafile}.filename];
         
         switch DISPLAY{iaxtemp}.displayfun{indstemp}
         case 'SISO',
            iresp = DISPLAY{iaxtemp}.iresp{indstemp};
            iref = DISPLAY{iaxtemp}.iref{indstemp};
            switch DISPLAY{iaxtemp}.specfun{indstemp}
            case {'FRF','Coherence','CPS','PSD Resp','Time History'},
               legtxt = [legtxt,',   Response DOF: ',num2str(iresp)];
            end
            switch DISPLAY{iaxtemp}.specfun{indstemp}
            case {'FRF','Coherence','CPS','PSD Ref','Time History'},
               legtxt = [legtxt,',   Reference DOF: ',num2str(iref)];
            end
         end
       
         %     Add Legend to plot
         
         h1 = text(0.1,top-(2*ipos+1)*spc,legtxt);
         DISPLAY{iaxtemp}.hplttext{indstemp} = h1;
         set(h1,'fontsize',9,'interpreter','none')

         h2 = line(linspace(0,0.08,5),(top-(2*ipos+1)*spc)*ones(1,5));
         DISPLAY{iaxtemp}.hplttextln{indstemp} = h2;
         set(h2,'LineStyle',DISPLAY{iaxtemp}.lnst{indstemp},'Color',DISPLAY{iaxtemp}.lnco{indstemp},'marker',DISPLAY{iaxtemp}.marker{indstemp});
         
      end
   end
end

%
%  ALL DONE
%

return