function out=execute(c)
%EXECUTE generates report contents
%   OUT=EXECUTE(C)

%   Copyright (c) 1997-2000 by The MathWorks, Inc.
%   $Revision: 1.4 $  $Date: 2000/06/09 19:47:16 $

currContext=getparentloop(c);

if c.att.isSinglePlot | c.att.isSingleTable
   singleList=searchblocktype(c.zslmethods,...
      {'BlockType','Lookup'},...
      currContext);
   numSingle=length(singleList);
else
   singleList={};
   numSingle=0;
end

if c.att.isDoublePlot | c.att.isDoubleTable
   doubleList=searchblocktype(c.zslmethods,...
      {'BlockType','SubSystem','MaskType','Lookup Table (2-D)'},...
      currContext);
   numDouble=length(doubleList);
else
   doubleList={};
   numDouble=0;
end

if numSingle+numDouble>0
   %if any candidate blocks were found
   
   if (c.att.isSinglePlot & numSingle>0) | ...
         (c.att.isDoublePlot & numDouble>0)
      %if any images will be drawn, initialize the image comp
      drawFig=figure('IntegerHandle','off',...
         'HandleVisibility','on',...
         'InvertHardcopy','off',...
         'MenuBar','none',...
         'Name','Report Generator Lookup Table Plot',...
         'Tag','RPTGEN LOOKUP TABLE FIGURE',...
         'Color',[1 1 1],...
         'Visible','off');
      figSnap=c.rptcomponent.comps.chgfigsnap;
      
      att=figSnap.att;
      att.isCapture=logical(0);
      att.ImageFormat=c.att.ImageFormat;
      att.FigureHandle=drawFig;
      att.PaperOrientation=c.att.PaperOrientation;
      att.isResizeFigure='manual';
      att.PrintSize=c.att.PrintSize;
      att.PrintUnits=c.att.PrintUnits;
      figSnap.att=att;
   else
      figSnap.att.ImageTitle='';
      drawFig=[];
   end 
      
   if (c.att.isSingleTable & numSingle>0) | ...
         (c.att.isDoubleTable & numDouble>0)
      tableComp=c.rptcomponent.comps.cfrcelltable;
      %if any tables will be drawn, initialize the table comp
      tableComp.att.isPgwide=0;
   else
      tableComp.att.TableTitle='';
   end
   
   %Produce output
   out={};
   for i=1:numSingle
      [xVal,xName]=LocParamValue(singleList{i},'InputValues');
      [yVal,yName]=LocParamValue(singleList{i},'OutputValues');
      
      if ~isempty(xVal) & length(xVal)==length(yVal)
         blkTitle=LocBlockTitle(c,singleList{i});
         tableComp.att.TableTitle=blkTitle;
         if c.att.isSinglePlot
            if LocSinglePlot(singleList{i},drawFig,c.att.SinglePlotType,...
                  xVal,xName,yVal,yName);
               figSnap.att.ImageTitle=blkTitle;
               out{end+1}=runcomponent(figSnap,0);
               tableComp.att.TableTitle='';
            end %if plotting
         end
         if c.att.isSingleTable
            tableComp.att.numHeaderRows=1;
            tableComp.att.TableCells=LocSingleTable(xVal,yVal);
            out{end+1}=runcomponent(tableComp);
         end %if table 
      else
         status(c,LocWarning(singleList{i}),2);
      end %if ok
   end %loop over all singles
   
   for i=1:numDouble
      [xVal,xName]=LocParamValue(doubleList{i},'x');
      [yVal,yName]=LocParamValue(doubleList{i},'y');
      [tVal,tName]=LocParamValue(doubleList{i},'t');
      
      xLength=length(xVal);
      yLength=length(yVal);
      tSize=size(tVal);
      
      if ~isempty(tVal) & xLength*yLength==prod(tSize)
         blkTitle=LocBlockTitle(c,doubleList{i});
         tableComp.att.TableTitle=blkTitle;
         if c.att.isDoublePlot
            if LocDoublePlot(doubleList{i},drawFig,c.att.DoublePlotType,...
                  xVal,xName,yVal,yName,tVal,tName);
               figSnap.att.ImageTitle=blkTitle;
               out{end+1}=runcomponent(figSnap,0);
               tableComp.att.TableTitle='';
            end
         end %if creating plots
         if c.att.isDoubleTable
            tableComp.att.numHeaderRows=0;
            tableComp.att.TableCells=LocDoubleTable(xVal,yVal,tVal);
            out{end+1}=runcomponent(tableComp);
         end %if 
      else
         status(c,LocWarning(singleList{i}),2);
      end
   end
   
   delete(drawFig);
else
   out='';
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ok=LocSinglePlot(blkName,h,plotType,xVal,xName,yVal,yName);

delete(allchild(h));
axHandle=axes('Parent',h,...
   'HandleVisibility','on',...
   'Box','on',...
   'Color',[1 1 1],...
   'XlimMode','auto',...
   'YlimMode','auto');

ok=logical(0);

switch plotType
case 'lineplot'
   try
      lnHandle=line('Parent',axHandle,...
         'Xdata',xVal,...
         'Ydata',yVal,...
         'Color',[0 0 1],...
         'LineWidth',2,...
         'Marker','.');
      ok=logical(1);
   end
case 'barplot'
   try
      bar(xVal,yVal);
      ok=logical(1);
   end
end

if ok
   axProp=struct('FontAngle',  get(axHandle, 'FontAngle'), ...
      'FontName',   get(axHandle, 'FontName'), ...
      'FontSize',   get(axHandle, 'FontSize'), ...
      'FontWeight', get(axHandle, 'FontWeight'));
   
   allPorts=get_param(blkName,'PortHandles');
   
   %Attach labels to axes based on signal or variable names
   set(get(axHandle,'xlabel'),axProp,'String',...
      LocAxLabel(xName,allPorts.Inport,'Input Values'));
   set(get(axHandle,'ylabel'),axProp,'String',...
      LocAxLabel(yName,allPorts.Outport,'Output Values'));
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ok=LocDoublePlot(blkName,h,plotType,xVal,xName,yVal,yName,tVal,tName)


%plotType = multilineplot or surfaceplot
delete(allchild(h));
axHandle=axes('Parent',h,...
   'Box','off',...
   'Color',[1 1 1],...
   'Xgrid','on',...
   'Ygrid','on',...
   'Zgrid','on',...
   'ZlimMode','auto',...
   'XlimMode','auto',...
   'YlimMode','auto');

if strcmp(plotType,'surfaceplot')
    try
        set(axHandle,'View',[-37.5,30]);
        
        surfHandle=surface(yVal,xVal,tVal,...
            'FaceColor','interp',...
            'Parent',axHandle);
        ok=logical(1);
    catch
        ok=logical(0);
    end
else
    try
        plotHandles=plot(xVal,tVal,'parent',axHandle);

        legendLabels=cellstr(num2str(yVal(:)));
        
        legend(plotHandles,legendLabels,-1);
        
        ok=logical(1);
    catch
        ok=logical(0);
    end
end

if ok
   axProp=struct('FontAngle',  get(axHandle, 'FontAngle'), ...
      'FontName',   get(axHandle, 'FontName'), ...
      'FontSize',   get(axHandle, 'FontSize'), ...
      'FontWeight', get(axHandle, 'FontWeight'));
   
   allPorts=get_param(blkName,'PortHandles');
   
   
   set(get(axHandle,'xlabel'),axProp,'String',...
      LocAxLabel(yName,allPorts.Inport(2),'Column'));

  if strcmp(plotType,'surfaceplot')
      set(get(axHandle,'zlabel'),axProp,'String',...
          LocAxLabel(tName,allPorts.Outport,'Table'));
      set(get(axHandle,'ylabel'),axProp,'String',...
          LocAxLabel(xName,allPorts.Inport(1),'Row'));
  else
      set(get(axHandle,'ylabel'),axProp,'String',...
          LocAxLabel(tName,allPorts.Outport,'Table'));
      
  end
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  tCells=LocSingleTable(xVal,yVal);

tCells=[{'Input Value','Output Value'};...
      num2cell([xVal(:),yVal(:)])];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  tCells=LocDoubleTable(xVal,yVal,tVal);

tCells=[{''},num2cell(yVal(:)');...
      num2cell([xVal(:),tVal])]';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function warnStr=LocWarning(blkName);

warnStr=sprintf('Warning - could not draw %s lookup table',...
      strrep(blkName,sprintf('\n'),' '));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [pVal,pString]=LocParamValue(blkName,pName)

try
   pString=get_param(blkName,pName);
   pVal=evalin('base',pString);
catch
   pString=[];
   pVal=[];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function labelString=LocAxLabel(varName,portH,labelName)

labelString='';

avN=abs(varName);
if any(find((avN>='a' & avN<='z') | ...
      (avN>='A' & avN<='Z')))
   %we'll assume that the property string is actually
   %the proper axes label
   labelString=varName;
else
   try
      portType=get_param(portH,'porttype');
   catch
      portType='not found';
   end
   
   switch portType
   case 'outport'
      try
         labelString=get_param(portH,'Name');
      end
   case 'inport'
      try
         lineH=get_param(portH,'Line');
         srcH=get_param(lineH,'SrcPortHandle');
         labelString=get_param(srcH,'Name');
      end
   end
end

if isempty(labelString)
   labelString=labelName;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imTitle=LocBlockTitle(c,blkName);

switch c.att.TitleType
case 'none'
   imTitle='';
case 'auto'
   try
      imTitle=get_param(blkName,'Name');
   catch
      imTitle=blkName;
   end
otherwise
   imTitle=c.att.TitleString;   
end

