function DrawParticleFilterResults(filename,stepsave,add)

% DrawParticleFilterResults: Draw the results of the partice filter based on saved data 
%
% DrawParticleFilterResults(filename,stepsave,add)
% filename: file with the results
% stepsave: steps where we want figures file to be store in a graphic
%           interfase
% add: String array with the labels to be use in the saved files


% APRENDIZAJE AUTOMÁTICO (G. Pajares y J. M. de la Cruz, Eds.)
% Editorial: RA-MA
% Capítulo 8: Monte Carlo II
% Autores: Eva Besada-Portas, Sergey M. Plis

if nargin<2
    stepsave=[];
end


load(filename,'xreal','dt');
hm=size(xreal,3);

dosave=zeros(hm,1);
dosave(stepsave)=1;
l=1;
for k=1:hm
    try
    warning off
    eval(sprintf('PF=load(filename,''x%d'',''w%d'');x=PF.x%d;w=PF.w%d;',k,k,k,k));
    warning on
    catch
        warning on
        return;
    end
    k2=find(w>max(w)/2);

    if k==1
        hf=figure;
        h(1)=plot(x(k2,1,k),x(k2,2,k),'*r');
        hold on;
        h(2)=plot(xreal(:,1,k),xreal(:,2,k),'*b','MarkerSize',8);
        h(3)=plot(xreal(:,1,k),xreal(:,2,k),'b','LineWidth',2);
        xmean=ObtainMeanTrajectory(x,w);
        h(4)=plot(xmean(1),xmean(2),'g','LineWidth',2);
        Height(1);
        MapLight(1);
        xlabel('X (m)','FontSize',12);
        ylabel('Y (m)','FontSize',12);
    else
        figure(hf);
        set(h(1),'XData',x(k2,1,k),'YData',x(k2,2,k));
        set(h(2),'XData',xreal(:,1,k),'YData',xreal(:,2,k));
        set(h(3),'XData',squeeze(xreal(:,1,1:k)),'YData',squeeze(xreal(:,2,1:k)));
        xmean=ObtainMeanTrajectory(x,w);
        set(h(4),'XData',xmean(1,:),'YData',xmean(2,:));
    end
    drawnow
    title(sprintf('Tiempo=%4.1f (s)',(k-1)*dt),'FontSize',12);
    if dosave(k)
       saveas(gcf,sprintf('%s_%d',filename,k),'fig')
       open(sprintf('%s_%d.fig',filename,k));
       set(get(get(gcf,'Children'),'Title'),'String',[add{l},get(get(get(gcf,'Children'),'Title'),'String')]);
       set(get(get(gcf,'Children'),'Ylabel'),'FontSize',18)
       set(get(get(gcf,'Children'),'Xlabel'),'FontSize',18)
       set(get(get(gcf,'Children'),'Title'),'FontSize',24)
       saveas(gcf,sprintf('%s_%d',filename,k),'fig')
       l=l+1;
       drawnow
       print(gcf,'-djpeg75',sprintf('%s_%d',filename,k))
       print(gcf,'-depsc',sprintf('%s_%d',filename,k))
    end
end

