function viewdist
A = dir('dcinj.*.001');
A = sort({A.name});
s = size(A);

a = 0;

for ii = 1:s(2)
    if(~isempty(findstr('Log',A{ii})))
        a = ii-1;
        break;
    end
end

if(a == 0)
    error('cannot find phase space dumps. Did you run Astra?')
end

ii = 0;

reply = 'something';

while ~strcmp(reply, 'q')
if(ii >= a) 
    ii = 0;
end
ii = ii + 1;
C = load(A{ii});

if (isempty(findobj('Tag','Dist')))
   scrsz = get(0,'ScreenSize');
   figure('Position',[scrsz(3)/(2-0.4) 1 scrsz(3)/3 scrsz(4)/1.2], 'Tag', 'Dist', 'Name', 'viewdist');
else
    figure(findobj('Tag', 'Dist'));
end

subplot(2,1,1)
plot(C(2:end,1),C(2:end,4),'.b');
title(['Z = ' num2str(C(1,3)) ' m, \epsilon_x = ' num2str(emit(C(2:end,1),C(2:end,4))/0.511) ' mm-mrad'])
xlabel('x (m)')
ylabel('p_x (eV/c)')
subplot(2,1,2)
plot(C(2:end,3),C(2:end,6),'.b');
title(['P_Z = ' num2str(C(1,6)/1E6) 'MeV/c, \epsilon_z = ' num2str(emit(C(2:end,3),C(2:end,6))) ' mm-keV/c'])
xlabel('z (m)')
ylabel('\Deltap_z (eV/c)')

reply = input('q to quit; next [Ret]?','s');
end

function v = emit(X, P)
X = X - mean(X);
P = P - mean(P);

v = sqrt(mean(X.^2)*mean(P.^2)-mean(X.*P)^2);