MATLAB Examples

write_xyz_traj.m

Contents

Examples

  • write_xyz_traj(atom,traj,Box_dim,filename_out)
function write_xyz_traj(atom,traj,Box_dim,filename_out)


if regexp(filename_out,'.xyz') ~= false;
    filename_out = filename_out;
else
    filename_out = strcat(filename_out,'.xyz');
end

Frames=size(traj,1);
nFrames=1:Frames;
nAtoms=size(atom,2);
Atom_section=cell(nAtoms,10);

fid = fopen(filename_out, 'W');
tic
for t=1:length(nFrames)

    %     Title='.xyz traj generated by matlab, t=';
    %     TitleTot={Title t};
    %     fprintf(fid,'%-40s%10.5f \r\n',TitleTot{:});
    fprintf(fid, '%-5i\r\n',nAtoms);

    b=t; if size(Box_dim,1)==1; b=1; end
    if size(Box_dim(b,:),2) == 3 || Box_dim(b,4) == 0 && Box_dim(b,5) == 0 && Box_dim(b,6) == 0;
        fprintf(fid, '# %10.5f%10.5f%10.5f\n\r',Box_dim(b,1:3)/10);
    else
        fprintf(fid, '# %10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f\r\n',Box_dim(b,:)/10);
    end

    for i = 1:nAtoms
        Atom_section(i,1:4) = [atom(i).type, traj(t,1+3*(i-1)), traj(t,2+3*(i-1)), traj(t,3+3*(i-1))];
        fprintf(fid, '%-5s%10.5f%10.5f%10.5f\r\n', Atom_section{i,1:4});
    end

    if mod(t,10)==0;
        t
    end

end
toc
fclose(fid);

sprintf('.xyz file exported %d frames', nFrames(end))