function [ ] = fplotCodesComparison(fieldX,fieldY,Codes,legds,colrs,sty,scaleX,scaleY,limX,limY,labX,labY,varargin)



% Complex example 
%
% for 3D plot, BEM is generated by fWTSimulation
% figure, fplotCodesComparison('r','CTloc',BEM,legds,colrs,sty,1,1,[],[],'','',BEM.WS,'WS')
% for a normal 2D plot simply remove the two las
% figure, fplotCodesComparison('r','CTloc',BEM,legds,colrs,sty,1,1,[],[],'','',BEM.WS,'WS')
% 

nCodes=length(Codes);

%% Let start by dealing with incomplete input
if(isempty(labY))
    labY=fieldY;
end
if(isempty(labX))
    labX=fieldX;
end
if(isempty(sty))
    sty={'-','--','-.',':'};
end
if(isempty(colrs))
    colrs=fColrs();
end

bOneCodeMode=0;


%% First of all if Codes is not a Cell, then it's probably an array of structure, let's convert it
if(~iscell(Codes))
    S=size(Codes);
    if(length(S)==2 && S(2)==1)
        if(S(1)==1)
            error('not done yet');
        else
            %         Codes={Codes};
            fprintf('reshaping\n');
            CodesB=Codes;
            Codes=cell(1,S(1));
            for i=1:S(1)
                Codes{i}=CodesB(i);
            end
        end
    else
        error('not done yet')
    end
    bOneCodeMode=1;

end 
if( isempty(legds) && bOneCodeMode && length(varargin)>0 )
    legds=textscan(sprintf('%d ',varargin{1}),'%s');
    legds=legds{1}'
end


%% Let's loop so that we have the data already;
for i=1:nCodes
    Code=Codes{i};
        X{i}=getfieldNested(Code,fieldX);
        Y{i}=getfieldNested(Code,fieldY);
        maxX(i)=max(X{i});
        minX(i)=min(X{i});
        maxY(i)=max(Y{i});
        minY(i)=min(Y{i});
end



if isstr(scaleX) || isstr(scaleY)
   switch scaleX
        case 'scaleByMax'
            scaleX=1./maxX;
        case 'scaleBySup'
            scaleX=repmat(1./max(maxX),1,nCodes);
    end
   switch scaleY
        case 'scaleByMax'
            scaleY=1./maxY;
        case 'scaleBySup'
            scaleY=repmat(1./max(maxY),1,nCodes);
    end
end
if length(scaleX)~=nCodes
    scaleX=repmat(scaleX,1,nCodes);
end
if length(scaleY)~=nCodes
    scaleY=repmat(scaleY,1,nCodes);
end

if( length(varargin)>1  )
    % we do 3d plot
    if(~isempty(varargin))
        param=varargin{1};
        labZ=varargin{2};
    else
        param=1:nCodes
        labz='';
    end
    nx=length(X{1});
    
    XX=zeros(nx,nCodes);
    YY=zeros(nx,nCodes);
    ZZ=zeros(nx,nCodes);
%     keyboard
    for i=1:nCodes
        XX(:,i)=X{i}(:)*scaleX(i);
        ZZ(:,i)=Y{i}(:)*scaleY(i);
        YY(:,i)=repmat(param(i),nx,1);
    end
    surf(XX,YY,ZZ);

    if(~isempty(limY))
        zlim(limY)
    end

    zlabel(labY)
    ylabel(labZ)
else


    % now for the plotting
    hold on,box on,grid on
    for i=1:nCodes
        plot(X{i}*scaleX(i),Y{i}*scaleY(i),sty{ mod(i-1,length(sty))+1 },'Color',colrs(mod(i-1,length(colrs(:,1)))+1,:))
    end

    if(~isempty(legds))
        legend(legds,0)
    end
    if(~isempty(limY))
        ylim(limY)
    end

    ylabel(labY)

end

if(~isempty(limX))
    xlim(limX)
end


xlabel(labX)

title(sprintf('Fig-%s-Vs-%s',labY,labX))
