How to loop function for all timesteps in one text file
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
phillip tomich
le 28 Jan 2023
Commenté : phillip tomich
le 29 Jan 2023
I need to find the contact area of a plane on a sphere, I have output this into the following file, it can either be one file or many in which the formatting is name.#, for example contact.8 would be the 8th timestep and have all of the atom locations from OVITO.
here is an example of each block
56
Lattice="157.1173913191 0.0 0.0 0.0 157.1686132955 0.0 0.0 0.0 158.0009824103" Origin="-78.5903792455 -78.5409169553 -79.1115056148" Properties=pos:R:3
-6.10662 78.4182 -8.17906
-2.09466 78.4527 -8.19804
-0.054071 78.4221 -10.1608
-8.11471 78.423 -6.1137
-10.1626 78.4656 -4.11985
I want to skip the first two lines in each block and run through some calculations on just the three columns of data then output it into an array to then copy and paste it into excel. Here is my code so far that works for one single file, I am trying to make it work for either one large file with every timestep or for a folder containing each timestep as an individual file. I attempted to follow other examples on here but could not get it to work.
fid = fopen(filename, 'rt');
C = textscan(fid, '%f%f%f', 'MultipleDelimsAsOne', true, 'Delimiter', '[;', 'Headerlines', 2);
fclose(fid);
x = C{1};
z = C{3};
xavg = mean(x);
zavg = mean(z);
x1 = (x - xavg).^2;
z1 = (z - zavg).^2;
sumxz = x1 + z1;
avgxz = mean(sumxz);
contactradius = sqrt(avgxz.*2); %in Angstrom
And then here is what I have attempting to loop through a large file containing all of the timesteps. It seems as though it does not actually pull the data from the blocks.
content = fileread(filename);
%%
blockEnds = strfind(content, 'Lattice') - 3;
blockEnds = [blockEnds(2:end),numel( content )];
blockStarts = strfind(content, 'R:3' ) + 1;
nBlocks = numel(blockStarts);
data = cell(nBlocks, 1);
fprintf( '%d blocks found. \n', nBlocks);
for bId = 1 : nBlocks
data{bId} = reshape( sscanf( content(blockStarts(bId):blockEnds(bId)), '%f'), 3, [] ).';
dat = data{bId};
x = dat{1};
z = dat{3};
xavg = mean(x);
zavg = mean(z);
x1 = (x - xavg).^2;
z1 = (z - zavg).^2;
sumxz = x1 + z1;
avgxz = mean(sumxz);
contactradius = sqrt(avgxz.*2); %in Angstrom
end
2 commentaires
Réponse acceptée
Stephen23
le 28 Jan 2023
T = fileread('atomlocation.txt');
C = regexp(T,'(^\S+\s+\S+\s+\S+\s*\n)+','lineanchors','match')
N = numel(C);
fprintf('%d blocks found.\n',N);
V = nan(1,N); % store the output
for k = 1:N
C(k) = textscan(C{k},'%f%f%f','CollectOutput',true);
X = C{k}(:,1);
Z = C{k}(:,3);
Xavg = mean(X);
Zavg = mean(Z);
X1 = (X - Xavg).^2;
Z1 = (Z - Zavg).^2;
sumxz = X1 + Z1;
avgxz = mean(sumxz);
V(k) = sqrt(avgxz.*2); % contactradius in Angstrom
end
display(V)
All of the numeric arrays are stored in C, for you to take a look at (confirm they are the correct values, further calculations, etc). The contactradius data are stored in V, for you to save in a file or whatever.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Low-Level File I/O dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!