%
%  plot_loglog_function_h2.m  ver 1.2  by Tom Irvine
%
function[fig_num,h2]=plot_loglog_function_h2(fig_num,x_label,y_label,t_string,ppp,fmin,fmax)
%
f=ppp(:,1);
a=ppp(:,2);
%
h2=figure(fig_num);
fig_num=fig_num+1;
%
plot(f,a)
%
ylabel(y_label);   
xlabel(x_label);
t_string=strrep(t_string,'=1 years','=1 year');
t_string=strrep(t_string,'= 1 years','= 1 year');
title(t_string);
set(gca,'MinorGridLineStyle',':','GridLineStyle',':','XScale','log',...
                     'YScale','log','XminorTick','off','YminorTick','off');
%
set(gca,'XGrid','on','GridLineStyle',':');
set(gca,'YGrid','on','GridLineStyle',':');

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

[~,ii]=min(abs(f-fmin));
[~,jj]=min(abs(f-fmax));

a=a(ii:jj);

ymax= 10^ceil(log10(1.1*max(a)));
%
ymin=ymax;
%
%% out1=sprintf('ymin=%8.4g  fmin=%8.4g  fmax=%8.4g',ymin,fmin,fmax);
%% disp(out1);
%
for i=1:length(a)
    if(ymin>a(i) && f(i)>=fmin && f(i) <=fmax)
        ymin=a(i);
    end
end
%
ymin= 10^floor(log10(ymin));
%
if(ymin==ymax)
    ymin=ymin/10;
    ymax=ymax*10;
end    
%

[xtt,xTT,iflag]=xtick_label(fmin,fmax);

if(iflag==1)
    set(gca,'xtick',xtt);
    set(gca,'XTickLabel',xTT);
    fmin=min(xtt);
    fmax=max(xtt);    
end

%
set(gca,'MinorGridLineStyle',':','GridLineStyle',':','XScale','log',...
                     'YScale','log','XminorTick','off','YminorTick','off');
                 
%
axis([fmin,fmax,ymin,ymax]);

return

% ---------- fonts & export size (to keep 9-pt exact in Word) ----------
FS = 8;                      % point size
FN = 'Arial';      % Word-safe mapping (more reliable than 'Times')
W_in = 4.20; H_in = 3.20;    % exact physical size

% apply fonts to existing objects and defaults
set(gcf, 'DefaultAxesFontName', FN, ...
         'DefaultTextFontName', FN, ...
         'DefaultLegendFontName', FN, ...
         'DefaultAxesFontSize', FS, ...
         'DefaultTextFontSize', FS, ...
         'DefaultLegendFontSize', FS, ...
         'DefaultAxesTitleFontSizeMultiplier', 1, ...
         'DefaultAxesLabelFontSizeMultiplier', 1);

set(findall(gcf,'-property','FontUnits'), 'FontUnits','points');
set(findall(gcf,'-property','FontName'),  'FontName', FN);
set(findall(gcf,'-property','FontSize'),  'FontSize', FS);

% exact physical size with a small safety pad to avoid clipping strokes
set(gcf, 'Units','inches');
pos = get(gcf,'Position');
pos(3:4) = [W_in H_in];
set(gcf,'Position',pos);


ax = gca;

set(ax, 'Units','inches');
ti  = get(ax,'TightInset');   % [L B R T] inches
pad = 0.06;                   % tweak 0.03–0.10 if needed
set(ax,'LooseInset', max(ax.LooseInset, ti + pad));
set(ax,'Clipping','off');

% lock paper to figure size
set(gcf,'PaperUnits','inches',...
        'PaperPositionMode','manual',...
        'PaperPosition',[0 0 W_in H_in],...
        'PaperSize',[W_in H_in]);

set(gcf,'Renderer','painters');  % ensure vector output

% ---------- export (vector PDF + EMF) ----------
export_fig_to_pdf_emf('myFigure', W_in, H_in, FS, FN);

end % main function

