% oplot4core
myshks = strvcat(deblank(slab(jj,:)));
%keyboard
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

if ~isempty(jj);
    jj=sort(jj);
    if verbose
        if length(varargin)<4
        cloa=[]; cloa=input('Close open figures (default NOT close): ' );
        else
            cloa=varargin{4};
        end
        if ~isempty(cloa)& cloa~=0; close all; end;
        if length(varargin)<5
        zro=input('Plot zero line (1/0) >> ');
        else
            zro=varargin{5};
        end
        if length(varargin)<6
        relative=input('Divide by ss values? (1/0)[default 0] >>');
        else
            relative=varargin{6};
        end
        if length(varargin)<7
        coind=(input('Line color index (ex 1; default black): '));
        else
            coind=varargin{7};
        end
        if isempty(coind); coind=1; end;
        if isempty(relative);relative=0;end


        if length(varargin)<0
        figs=(input('Enter directory/figure suffix (any string): ','s'));%fs='';
        else 
            figs=[];
        end
    else
        cloa=0;
            figs=([fname_,'_','_fig',num2str(jj),'_']);%,nmfig

        zro=0;
        relative=0;
        coind=1;
        fs=myshks;
    end
    if ~exist('doHPD','var')
        doHPD=0;
    elseif isempty(doHPD);
            doHPD=0;
    elseif doHPD~=0
        vars=(fields(oo_.PosteriorIRF.dsge.Mean));
        for slabcount=1:size(slab,1);
            vars=unique(regexprep(vars,['\_',slab(slabcount,:)],''));

        end
        newtitle=regexprep(vars,'\_','\\_');
        vars=strvcat(vars);

    end

    %             if ~isempty(fs);
    %                 if ~exist([pwd,'/figures/',deblank(num2str(fs))],'dir');
    %                     mkdir([pwd,'/figures'],deblank(num2str(fs)));
    %                 end;%..\figures
    %             end
    % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

    nopans=norows*nocols;%

    lines = strvcat('-',':','--',':-'); % LineStyle {-} | -- | : | -. | none
    markers=strvcat('o','s','+','>','d');

    if length(ashks)>4
        lines=repmat(lines,round((length(ashks))/4+.51),1);
        markers=repmat(markers,round((length(ashks))/4+.51),1);
    end

    colors=['k','b','r','m'];

    whosannual={};%{'PII','pii','pii_s','pii_c','pii_c_s','pii_h', 'pii_f','R','R_s','ReR', 'ReR_s'};
    if verbose
        if length(varargin)==0
        tit=input(upper('Give title of set of figures  -->'),'s');
        end
    end

    % CLEANING THE LIST OF VARIABLES FROM NON-RESPONSE VARIABLES
    kk=size(vars,1);
    avars=[];
    anewtitle=[];
    z=1;
    for k=1:kk;
        cc=0;
        for j=jj;
            if doHPD==0
                if exist(deblank(strcat ( deblank(vars(k,:)) , '_', slab(j,:) ) ),'var')==1;
                    cc=cc+1;
                end;
            else
                if isfield(oo_.PosteriorIRF.dsge.Mean,deblank(strcat ( deblank(vars(k,:)) , '_', slab(j,:) ) ))
                    cc=cc+1;
                end;
            end

        end;
        %keyboard
        if cc>0;

            avars = strvcat(avars,deblank(vars(k,:)));
            anewtitle{z,1}=deblank(newtitle{k});
            z=z+1;
        end;
    end;
    newtitle=anewtitle;
    vars=avars;

    % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
    slabtex=[];
    for j=1:size(slab,1);
        slabtex=strvcat(slabtex,regexprep((slab(j,:)),'_','\\_'));
    end;
    % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

    nopers = horizon;%options_.irf - 1;
    novars=size(vars,1);
    nofigs=ceil(novars/nopans);
    nn=0;
    time=1:nopers;

    % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
    hhh=[];ffiigg=[];
    handle_subp=0;
            nmfig=['Shock_',myshks];%regexprep(,'\_','-')
    for kk=1:nofigs
        %keyboard

        figname=([fname_,'_','_fig',num2str(kk),'_',nmfig]);
        tmp=figure('Name',figname);
        ffiigg=[ffiigg;tmp];
        %                 if ~isempty(tit)
        %                     annotation('textbox',[.1,.5,.5,.5],'String',tit,'Color',[1,0,0],'LineStyle','none','FontSize', 18)
        %                 end
        ii=min(novars,nopans);%-nn

        for i=1:(ii)%
            if  i+nn<=size(vars,1)
                handle_subp(i,kk)=subplot(norows,nocols,i);
                
                % if i<(ii+1); %uncomment this if want table of parameters
                z=0;

                for j=jj
                    z=z+1;
                    %  keyboard
                    if doHPD~=0
                        if ~isfield(oo_.PosteriorIRF.dsge.Mean,deblank([ deblank(vars(i+nn,:)) , '_', slab(j,:) ] ))
                            tmpA=0*time(:);
                        else
                            oplot4assign;
                        end;
                    else
                        if ~exist(deblank([ deblank(vars(i+nn,:)) , '_', slab(j,:) ] ),'var');
                            tmpA=0*time(:);
                        else
                            oplot4assign;
                        end
                    end




                    tmpA = round(1e8*tmpA)/1e8; % this lines avoids that a response of 0.00001% looks non-zero...
                    %             if zro
                    if doHPD==0
                        if 0 % put markers (this generates an auxiliary line which messes up the legends
                    plot(time(1:freq:end),tmpA(time(1:freq:end),:),'LineStyle','none','Marker',markers(z),'MarkerSize',4,'Color',colors(coind));
                    hold on;
                        end
                    %             end
                    hh=plot(time,tmpA(time,:),'LineStyle',lines(z),'LineWidth',1.2,'Color',colors(coind));
                    else
                tmpA=tmpA;
                        ciplot(tmpA(:,1),tmpA(:,end),1:size(tmpA(:,1),1),colHPD)
                        hold on
                        hh=plot(tmpA(:,2),'LineStyle',lines(z),'LineWidth',3,'Color',colors(coind));
                    end

                    hhh=[hhh;hh];

                set(gca,'XMinorTick','on');
%                      set(gca,'xtick',[])s

                 

                    hold on;
                end; % jj end
                axis tight
                if i==(ii)& length(jj)>1;
 
                    legend(slabtex(jj,:),'Location','Best');
                end;%,'Orientation','horizontal'
                if zro
                    plot(time,0*time,'k');
                end
                xlim([0 nopers]);
                if tmp_a==4;
                    txt='^{*}';
                else
                    txt='';
                end
                if tmp_a==4;
                    titx=[[newtitle{i+nn}],' (annualized)'];
                else
                    titx=[newtitle{i+nn}];
                end
                title( titx ,'FontSize',10);%regexprep(strcat(vars(i+nn,:)),'_','\\_')
                % xlabel('quarters');
                axis tight
% % % % % % % % % the following gives scientific notation for ticks
%                                      limL=get(gca,'YLim');
%                                      
%                 set(gca,'YTick',[limL(1):diff(limL)/4:limL(end)])
%                 set(gca,'YTickLabel',sprintf('%10.2d|',[limL(1):diff(limL)/4:limL(end)]))
%                        grid on
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % 
                %             else; % output the parameter values % for table of parameters
                %                 ylim([0 9]);
                %                 for w=1:size(partext,1);
                %                  text(0,10-propw*w,partext(w,:),'FontSize',8);
                %                 end;supl
                %                 axis off;
                %             end; % if end
            end
%             if i>(norows-1)*nocols
                set(gca,'xtick',[0:freq:nopers],'FontSize',8);%grid on;
%             end
               
        end; % i end
%         [a3,h3]=suplabel(['Shock: ',regexprep(myshks,'\_','-')],'t');
%         set(h3,'FontSize',16)

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        nn=nn+nopans;

        % saving  figures
        if ~isempty(figs)
if ~exist('figures','dir')
    mkdir('figures');
end

            figname=([pwd,'/figures','/',figs,nmfig,'_',num2str(kk)]);
            print('-djpeg','-r200',figname);
            %         print('-dpdf','-r600',figname);
            print('-deps2','-r600',figname);
            [a,b]=system(['epstopdf ',[figname,'.eps']]);
            %if epstopdf is not installed use the following (the margins are not optimal though)
            %     [a,b]=system(['ps2pdf ',[nmsave,'.eps']]);
            saveas(gcf,figname,'fig')
            if verbose
                pathfig=input(upper('save copy of figures somewhere else? --> (press enter or give path)  '),'s');
            else
                pathfig=[];
            end
            if ~isempty(pathfig);
                if ~exist([deblank(num2str(pathfig))],'dir');
                    disp('Directory does not exist');
                    %       eval([ '!md ', deblank(num2str(pathfig)),'\figures']);
                else;%..\figures
                    saveas(gcf,[pathfig,'/',nmfig,'_',num2str(kk)],'fig');
                    print('-deps2',[pathfig,'/',nmfig,'_',num2str(kk)]);
                end
            end

            %         eval(['!copy param_oil.m D:\codes_oil112205\figures\',num2str(fs),'\param_oil.m']);
        end;

    end; % kk end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % extra figure with params only
    figon=0;
    if figon==1;
        figure;      ylim([0 size(partext,1)]);
        for w=1:size(partext,1);
            text(0,size(partext,1)+1-propw*w,partext(w,:),'FontSize',14);
        end;
        axis off;
    end;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end; %[if jj not empty]

% if ~isempty(fs)
%     disp('Copied param_oil.m. ');
% %         eval(['!copy param_oil.m D:\codes_oil112205\figures\',num2str(fs),'\param_oil.m']);
%         copyfile('param_oil.m',['figures\',num2str(fs),'\param_oil.m'])
% end;
if length(varargin)==0
    
if verbose
    prfi=input(upper('Print Figures? (y/n)'),'s');
    savehand=input('Save figure handle? (y/n)','s');
else
    prfi=1;
    savehand=0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% saving figure handle


if isempty(savehand);savehand='n';end
if deblank(lower(savehand))=='y';
    if ~exist('../figures','dir');
        mkdir('../figures');
    end
    specname=input('Give tag for this set of handles  >> ','s');
    allirf=struct('IRFs',M_.fname);
    for jj=1:size(vars,1);
        allirf=setfield(allirf,[deblank(vars(jj,:)),'_',myshks(1,:)],eval([deblank(vars(jj,:)),'_',myshks(1,:)]));

        %         allirf{jj,2}=eval([deblank(vars(jj,:)),'_',myshks(1,:)]);
    end
    eval(['save ../figures/handle_subp_', M_.fname,myshks,'_',specname,'.mat allirf handle_subp -MAT']);
    disp(['Handle saved as ','''../figures/handle_subp_', M_.fname,myshks,'_',specname,'.mat'''])
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isempty(prfi);prfi='n';end

if deblank(lower(prfi))=='y';

    for jj=ffiigg'
        orient(figure(jj),'landscape')
        %  set(figure(jj),'PaperOrientation','landscape')
        print(['-f',num2str(figure(jj))])
        %         orient portrait
    end
end

disp(upper('IRFs are NOT in % '))
end