% 
% Reads variables from strplt.rxx file, and plots sL vs Imag
% 27-Feb-2006 Achim Weidemann
%
function strplotnfit(filename)
ff=['S:\MagData\E163\quad\QE-N3\' filename]
%filename='strplt.r27';
[Imag, sImag, SL, sSL, TF, sTF, THsp, sTHsp]...
 =textread(ff, '%f %f %f %f %f %f %f %f','headerlines',1);
%%%%%%
p=polyfit(Imag,SL,3)
SLF=polyval(p,Imag);
SLres=SL-SLF;
[nobs, dim1] =size(Imag);
lsq=norm(SLres)/nobs
pstring = ['3rd o poly: ' num2str(p,5)] 
qstring = ['sqrt(res^2)/n = ' num2str(lsq,'%11.5g')]
%%%%%%%%%%%%%
figure(1)
subplot(2,1,1)
plot(Imag,SL,'-ob','LineWidth',1);
hold on
plot(Imag,SLF,'+r','LineWidth',1);
title(['Int. Strength vs Current-' filename]);
xlabel('Current / A')
ylabel('Int. Strength / T')
legend1 = legend('Data','Fit', 'Location','SouthEast');
text(0.,2.8,pstring)
text(0.,2.5,qstring)
grid on
subplot(2,1,2)
plot(Imag,SLres,'-+r','LineWidth',1);
title('Residuals');xlabel('Current / A')
legend1 = legend('Residuals', 'Location','NorthEast');
% text(-0.005,60.,qstring) put in above legend
grid on
fig1name=[filename,'Fig1.jpg']
print( '-djpeg', fig1name)
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(2)
subplot(2,1,1)
plot(Imag,SL,'-ob','LineWidth',1)
title(['Int. Strength vs Current (for current<3A)-' filename]);
xlabel('Current / A')
ylabel('Int. Strength / T')
hold on
plot(Imag,SLF,'+r','LineWidth',1);
axis([0.0 3.0 0.0 0.1])
legend1 = legend('Data','Fit','Location', 'NorthWest');
grid on
subplot(2,1,2)
plot(Imag,SLres,'-+r','LineWidth',1);
axis([0.0 3.0 -0.005 0.005])
title('Residuals');xlabel('Current / A')
legend1 = legend('Residuals', 'Location','NorthEast');
grid on
fig2name=[filename,'Fig2.jpg']
print( '-djpeg', fig2name)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%End