How to loop function for all timesteps in one text file
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!