%
%   subplot_magnitude_phase_two_function.m  ver 1.3  by Tom Irvine
%
function[fig_num]=...
    subplot_magnitude_phase_two_function(fig_num,t_string1,t_string2,...
                    fmin,fmax,ff1,FRF_p1,FRF_m1,ff2,FRF_p2,FRF_m2,ylab,md)
%


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

if(iflag==1)
    fmin=min(xtt);
    fmax=max(xtt);
end



hp=figure(fig_num);
fig_num=fig_num+1;
%
subplot(3,2,1);
plot(ff1,FRF_p1);
title(t_string1);
grid on;
ylabel('Phase (deg)');
%
%%%%%%
%
ylim([-180,180]);
    
if(iflag==1)
    set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0,90,180],'xtick',xtt,'XTickLabel',xTT);  
else

    set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0,90,180]);    
end
%
%%%%%%
%
if(max(FRF_p1)<=0.)
%
   ylim([-180,0]);
%
  if(iflag==1)
    set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0],'xtick',xtt,'XTickLabel',xTT);
  else
    set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0]);    
  end
end  
%
if(min(FRF_p1)>=-90. && max(FRF_p1)<90.)
%
    ylim([-90,90]);
    
    if(iflag==1)
        set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-90,0,90],'xtick',xtt,'XTickLabel',xTT);
    else
        set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-90,0,90]);        
    end
end 
%
if(min(FRF_p1)>=0.)
%
    ylim([0,180]);
    
    if(iflag==1)    
    
        set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[0,90,180],'xtick',xtt,'XTickLabel',xTT);
    else
     
        set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[0,90,180]);       
    end    
end 

try
    xlim([fmin,fmax]);
catch    
end


%
subplot(3,2,[3 5]);
plot(ff1,FRF_m1);
grid on;
xlabel('Frequency(Hz)');
ylabel(ylab);


[~,index1] = min(abs(ff1-fmin));
[~,index2] = min(abs(ff1-fmax));

ymin=min(FRF_m1(index1:index2));
ymax=max(FRF_m1(index1:index2));

NN=10^md;

ymax=10^(ceil(log10(ymax)));

if(ymin<ymax/NN)
    ymin=ymax/NN;
end

[ytt,yTT,iflag]=ytick_label(ymin,ymax);


if(iflag==1)
    set(gca,'ytick',ytt);
    set(gca,'YTickLabel',yTT);
    ylim([min(ytt),max(ytt)]);
end

if(iflag==1)
    set(gca,'MinorGridLineStyle',':','GridLineStyle',':','XScale','log',...
                     'YScale','log','XminorTick','off','YminorTick','off','xtick',xtt,'XTickLabel',xTT);
else
    set(gca,'MinorGridLineStyle',':','GridLineStyle',':','XScale','log',...
                     'YScale','log','XminorTick','off','YminorTick','off');    
end

%

try

    [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');
    %

    xlim([fmin,fmax]);
catch    
end

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

subplot(3,2,2);
plot(ff2,FRF_p2);
title(t_string2);
grid on;
ylabel('Phase (deg)');
%
%%%%%%
%
ylim([-180,180]);
    
if(iflag==1)
    set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0,90,180],'xtick',xtt,'XTickLabel',xTT);  
else

    set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0,90,180]);    
end
%
%%%%%%
%
if(max(FRF_p2)<=0.)
%
   ylim([-180,0]);
%
  if(iflag==1)
    set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0],'xtick',xtt,'XTickLabel',xTT);
  else
    set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0]);    
  end
end  
%
if(min(FRF_p2)>=-90. && max(FRF_p2)<90.)
%
    ylim([-90,90]);
    
    if(iflag==1)
        set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-90,0,90],'xtick',xtt,'XTickLabel',xTT);
    else
        set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-90,0,90]);        
    end
end 
%
if(min(FRF_p2)>=0.)
%
    ylim([0,180]);
    
    if(iflag==1)    
    
        set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[0,90,180],'xtick',xtt,'XTickLabel',xTT);
    else
     
        set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[0,90,180]);       
    end    
end 

try
    xlim([fmin,fmax]);
catch    
end


%
subplot(3,2,[4 6]);
plot(ff2,FRF_m2);
grid on;
xlabel('Frequency(Hz)');
ylabel(ylab);


[~,index1] = min(abs(ff2-fmin));
[~,index2] = min(abs(ff2-fmax));

ymin=min(FRF_m1(index1:index2));
ymax=max(FRF_m1(index1:index2));

NN=10^md;

ymax=10^(ceil(log10(ymax)));

if(ymin<ymax/NN)
    ymin=ymax/NN;
end

[ytt,yTT,iflag]=ytick_label(ymin,ymax);


if(iflag==1)
    set(gca,'ytick',ytt);
    set(gca,'YTickLabel',yTT);
    ylim([min(ytt),max(ytt)]);
end

if(iflag==1)
    set(gca,'MinorGridLineStyle',':','GridLineStyle',':','XScale','log',...
                     'YScale','log','XminorTick','off','YminorTick','off','xtick',xtt,'XTickLabel',xTT);
else
    set(gca,'MinorGridLineStyle',':','GridLineStyle',':','XScale','log',...
                     'YScale','log','XminorTick','off','YminorTick','off');    
end

%

try
    [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');
    %    
    xlim([fmin,fmax]);
catch    
end


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

set(hp, 'Position', [0 0 1050 550]);