MATLAB Examples

## import_atom_gro.m - This function import .gro files into the atom struct

```function atom = import_atom_gro_test(filenamegro) ```
```tic C = textread(filenamegro, '%s','delimiter', '\n'); Title=C{1}; nAtoms=str2num(cell2mat(C(2))); startRow = 3; endRow = 2+nAtoms; Box_dim=str2double(strsplit(C{endRow+1}))*10; if size(Box_dim,2)==3; Box_dim=[Box_dim 0 0 0]; end fileID = fopen(filenamegro,'r'); ```

## Read columns of data as strings:

```formatSpec = '%5s%5s%5s%5.0f%8.3f%8.3f%8.3f%8.4f%8.4f%8.4f%[^\n\r]'; ```

## Open the text file.

```fileID = fopen(filenamegro,'r'); dataArray = textscan(fileID, formatSpec, endRow-startRow+1, 'Delimiter', '', 'WhiteSpace', '', 'EmptyValue' ,NaN,'HeaderLines', startRow-1, 'ReturnOnError', false); fclose(fileID); ```

AtomID ResName

```nAtoms=size(dataArray{:,5}(:),1); MolID = str2double((dataArray{:,1})); % Converts to double Resname = strtrim(dataArray{:,2}); Atomtype = strtrim(dataArray{:,3}); ind=find(dataArray{:,4}(:)>99999); dataArray{1,4}(ind)=dataArray{1,4}(ind)-100000; AtomID = dataArray{:,4}; % Converts to double X_coord = dataArray{:,5}*10; Y_coord = dataArray{:,6}*10; Z_coord = dataArray{:,7}*10; X_velo = dataArray{:,8}*10; Y_velo = dataArray{:,9}*10; Z_velo = dataArray{:,10}*10; % atom = struct('atom',struct('molid',cell(1,nAtoms),'resname',cell(1,nAtoms),'type',cell(1,nAtoms),'fftype',cell(1,nAtoms),... % 'index',cell(1,nAtoms),'x',cell(1,nAtoms),'y',cell(1,nAtoms),'z',cell(1,nAtoms),'vx',cell(1,nAtoms),'vy',cell(1,nAtoms),'vz',cell(1,nAtoms))); ```
```nmol=1;first_in=[1];last_in=[]; for i=1:nAtoms; if i > 1 && MolID(i) ~= MolID(i-1) nmol=nmol+1; atom(i).molid=nmol; first_in(atom(i).molid,1)=i;last_in(atom(i).molid-1,1)=i-1; elseif i > 1 atom(i).molid=atom(i-1).molid; elseif i == 1 atom(i).molid=1; end atom(i).resname=Resname(i); atom(i).type=Atomtype(i); atom(i).fftype=Atomtype(i); atom(i).index=mod(i,100000); atom(i).neigh.type = {}; atom(i).neigh.index = zeros(6,1); atom(i).neigh.dist = zeros(6,1); atom(i).bond.type = zeros(6,1); atom(i).bond.index = zeros(6,1); atom(i).angle.type = zeros(6,1); atom(i).angle.index = zeros(6,1); atom(i).x=X_coord(i); atom(i).y=Y_coord(i); atom(i).z=Z_coord(i); % atom(i).fx=X_coord(i)/Box_dim(1); % atom(i).fy=Y_coord(i)/Box_dim(2); % atom(i).fz=Z_coord(i)/Box_dim(3); atom(i).vx=X_velo(i); atom(i).vy=Y_velo(i); atom(i).vz=Z_velo(i); end last_in(atom(end).molid,1)=nAtoms; XYZ_data=[[atom.x]' [atom.y]' [atom.z]']; XYZ_labels=[atom.type]'; assignin('caller','XYZ_labels',XYZ_labels) assignin('caller','XYZ_data',XYZ_data) assignin('caller','atom',atom) assignin('caller','nAtoms',nAtoms) assignin('caller','Box_dim',Box_dim) assignin('caller','MolID',MolID) disp('.gro file imported') toc ```