How to loop function for all timesteps in one text file

6 vues (au cours des 30 derniers jours)
phillip tomich
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
Stephen23
Stephen23 le 28 Jan 2023
@phillip tomich: please upload a sample data file by clicking the paperclip button.
phillip tomich
phillip tomich le 28 Jan 2023
Sorry just added it, thanks

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 28 Jan 2023
T = fileread('atomlocation.txt');
C = regexp(T,'(^\S+\s+\S+\s+\S+\s*\n)+','lineanchors','match')
C = 1×6 cell array
{'-2.31034 78.0166 -13.5594←↵-4.38853 78.0916 -11.6052←↵-6.43777 78.1311 -9.60072←↵-2.39288 78.1466 -9.53437←↵-0.335172 78.1691 -11.5136←↵-8.5106 78.1934 -7.57393←↵-10.5731 78.1979 -5.5872←↵-4.45782 78.2572 -7.56348←↵-6.46974 78.2934 -5.54469←↵-2.47624 78.3532 -5.50165←↵-0.391387 78.2833 -7.48523←↵-12.6193 78.2089 -3.56305←↵-8.53783 78.3107 -3.58222←↵-10.5804 78.3486 -1.55666←↵-4.47739 78.3542 -3.48997←↵-6.55298 78.3555 -1.50503←↵-2.47103 78.399 -1.4406←↵-0.454108 78.3725 -3.44696←↵-12.6362 78.3195 0.397645←↵-8.60731 78.4036 0.513466←↵-10.6749 78.3913 2.49431←↵-4.56983 78.4077 0.531213←↵-6.58649 78.4603 2.54015←↵-0.509969 78.4323 0.605243←↵-2.57048 78.4693 2.60087←↵-8.6719 78.4556 4.53709←↵-4.63189 78.4883 4.60463←↵-6.70157 78.4655 6.62884←↵-0.580785 78.4697 4.67298←↵-2.65546 78.4529 6.68634←↵-4.68551 78.4969 8.70342←↵-2.65341 78.5106 10.7081←↵-0.618942 78.5084 8.71149←↵1.65223 78.0659 -13.5302←↵1.66 78.2433 -9.48669←↵3.70223 78.1393 -11.5062←↵5.69187 78.1981 -9.39509←↵1.56898 78.3267 -5.38858←↵3.6662 78.2908 -7.41663←↵5.6264 78.3153 -5.34632←↵7.6775 78.2646 -7.34365←↵9.65158 78.3322 -5.243←↵1.53357 78.4061 -1.43359←↵3.57578 78.3547 -3.38084←↵5.54031 78.4752 -1.28848←↵7.58618 78.4054 -3.30267←↵9.58243 78.4365 -1.22916←↵11.6778 78.4261 -3.21454←↵1.476 78.5065 2.72531←↵3.49637 78.4652 0.663017←↵5.4818 78.4612 2.74995←↵7.54732 78.505 0.780878←↵9.55716 78.4715 2.79316←↵11.5679 78.4652 0.782689←↵1.44514 78.5158 6.71151←↵3.49403 78.5028 4.75316←↵5.43625 78.4986 6.78663←↵7.47524 78.5122 4.78636←↵3.40068 78.5036 8.79654←↵1.29899 78.4877 10.7844←↵'} {'-2.29259 78.0417 -13.4977←↵-4.37893 78.0801 -11.5416←↵-6.41748 78.0913 -9.52922←↵-2.41437 78.1933 -9.46953←↵-0.345385 78.1109 -11.4559←↵-8.48515 78.1441 -7.55883←↵-10.5365 78.1772 -5.60482←↵-4.43175 78.2051 -7.5115←↵-6.48025 78.2263 -5.53788←↵-2.46794 78.2868 -5.44667←↵-0.41581 78.2151 -7.43334←↵-12.5683 78.159 -3.58509←↵-8.55178 78.286 -3.55669←↵-10.5976 78.2614 -1.52319←↵-4.51775 78.2869 -3.46188←↵-6.59843 78.3653 -1.48633←↵-2.52914 78.3261 -1.40962←↵-0.468848 78.2778 -3.35424←↵-12.6755 78.2839 0.422929←↵-8.66323 78.3621 0.550132←↵-10.7301 78.3129 2.51892←↵-4.58044 78.398 0.566148←↵-6.65164 78.406 2.57875←↵-0.55349 78.3987 0.643514←↵-2.60956 78.4293 2.66062←↵-8.70954 78.4256 4.57679←↵-4.66711 78.4291 4.64345←↵-6.71957 78.4335 6.63762←↵-0.61683 78.4415 4.6878←↵-2.66676 78.4526 6.69094←↵-4.72212 78.4321 8.66426←↵-2.707 78.4608 10.6966←↵-0.640601 78.4561 8.74127←↵1.68201 78.0394 -13.458←↵1.67394 78.1846 -9.43489←↵3.6902 78.1188 -11.4292←↵5.71041 78.1972 -9.37965←↵1.56327 78.2971 -5.34848←↵3.66205 78.2284 -7.35668←↵5.61666 78.2934 -5.30572←↵7.64949 78.2178 -7.3347←↵9.60615 78.3356 -5.2379←↵1.46088 78.3345 -1.31201←↵3.54857 78.3155 -3.28579←↵5.53631 78.414 -1.2828←↵7.55997 78.3565 -3.2625←↵9.55631 78.3903 -1.21636←↵11.5894 78.3687 -3.17319←↵1.43301 78.4346 2.70843←↵3.4929 78.4095 0.724586←↵5.51417 78.4345 2.78399←↵7.54088 78.4606 0.796416←↵9.53807 78.4578 2.80233←↵11.5552 78.4214 0.823531←↵1.40464 78.4541 6.78219←↵3.45058 78.4467 4.76575←↵5.43931 78.4558 6.83387←↵7.51132 78.4439 4.82869←↵3.38809 78.4612 8.82533←↵1.28443 78.4529 10.8178←↵'} {'-2.31597 78.014 -13.4973←↵-4.38416 78.0485 -11.5135←↵-6.42386 78.0834 -9.55996←↵-2.43221 78.1945 -9.43014←↵-0.3924 78.154 -11.4105←↵-8.48182 78.1278 -7.56976←↵-10.5332 78.1814 -5.5785←↵-4.43913 78.1837 -7.45191←↵-6.4962 78.2034 -5.48824←↵-2.45364 78.2479 -5.39522←↵-0.42631 78.2377 -7.38886←↵-12.5544 78.1918 -3.52273←↵-8.52199 78.2716 -3.50749←↵-10.5808 78.243 -1.50728←↵-4.47976 78.2544 -3.4132←↵-6.52938 78.2806 -1.41727←↵-2.5032 78.2377 -1.33916←↵-0.454614 78.3047 -3.34443←↵-12.6262 78.2946 0.472903←↵-8.62968 78.3312 0.589964←↵-10.6616 78.3031 2.55335←↵-4.56623 78.3254 0.630499←↵-6.6374 78.3925 2.64572←↵-0.52787 78.3504 0.715129←↵-2.55906 78.3962 2.66533←↵-8.66959 78.3275 4.61169←↵-4.62497 78.3834 4.67619←↵-6.64995 78.4018 6.66878←↵-0.549031 78.3955 4.72368←↵-2.6258 78.4072 6.73791←↵-4.68274 78.3962 8.72699←↵-2.65651 78.4141 10.7273←↵-0.620728 78.4134 8.7347←↵1.63744 78.0857 -13.391←↵1.63353 78.2245 -9.37388←↵3.66769 78.1651 -11.3653←↵5.72634 78.1762 -9.36822←↵1.60873 78.2865 -5.35605←↵3.69971 78.2361 -7.33959←↵5.61753 78.3351 -5.24143←↵7.67092 78.2315 -7.27886←↵9.70142 78.3048 -5.24248←↵1.50582 78.3592 -1.24808←↵3.58415 78.3667 -3.26783←↵5.58931 78.4019 -1.24098←↵7.63952 78.3806 -3.24123←↵9.60509 78.3585 -1.14568←↵11.6508 78.3854 -3.09182←↵1.49521 78.3719 2.77034←↵3.54852 78.3928 0.791586←↵5.55212 78.3888 2.85354←↵7.55672 78.3743 0.87578←↵9.54016 78.4124 2.88388←↵11.6249 78.3739 0.846393←↵1.44372 78.4119 6.81571←↵3.51654 78.3985 4.84371←↵5.45223 78.4101 6.88831←↵7.53526 78.4182 4.9125←↵3.4229 78.4177 8.92363←↵1.3102 78.412 10.838←↵'} {'-2.23422 78.0819 -13.4724←↵-4.34173 78.095 -11.5018←↵-6.35706 78.1233 -9.46317←↵-2.3441 78.2336 -9.40631←↵-0.305138 78.1691 -11.4065←↵-8.43799 78.1951 -7.48251←↵-10.4741 78.2085 -5.4752←↵-4.37768 78.2498 -7.41059←↵-6.43753 78.2741 -5.41608←↵-2.40595 78.3243 -5.32784←↵-0.368163 78.2779 -7.31093←↵-12.5536 78.2361 -3.46708←↵-8.47832 78.2972 -3.40686←↵-10.5544 78.2915 -1.42374←↵-4.442 78.3087 -3.33653←↵-6.53479 78.3602 -1.33806←↵-2.45131 78.3492 -1.30006←↵-0.388372 78.3344 -3.28114←↵-12.544 78.3177 0.535417←↵-8.56871 78.3418 0.683842←↵-10.6194 78.3202 2.65066←↵-4.48073 78.3502 0.705207←↵-6.5251 78.356 2.71802←↵-0.478264 78.3386 0.750856←↵-2.53021 78.3279 2.77026←↵-8.6218 78.3468 4.70398←↵-4.57506 78.3487 4.75611←↵-6.60105 78.361 6.74399←↵-0.544658 78.3524 4.80413←↵-2.57545 78.372 6.79779←↵-4.58525 78.3647 8.79843←↵-2.64276 78.3654 10.8297←↵-0.627014 78.3586 8.85531←↵1.715 78.1235 -13.3681←↵1.6801 78.2691 -9.31245←↵3.7234 78.1707 -11.3491←↵5.72466 78.2036 -9.2745←↵1.63241 78.3104 -5.27114←↵3.67817 78.3106 -7.27336←↵5.71156 78.3384 -5.21265←↵7.73176 78.276 -7.23499←↵9.69446 78.318 -5.1727←↵1.60536 78.3477 -1.20711←↵3.64734 78.3264 -3.24294←↵5.61851 78.354 -1.16225←↵7.69454 78.3493 -3.14493←↵9.62956 78.3566 -1.05903←↵11.6441 78.3246 -3.04777←↵1.52149 78.3562 2.83993←↵3.55741 78.3444 0.850778←↵5.54651 78.3566 2.85126←↵7.60556 78.3614 0.918035←↵9.60144 78.369 2.93094←↵11.6419 78.349 0.93183←↵1.43003 78.3647 6.85907←↵3.51959 78.3631 4.88471←↵5.49624 78.3556 6.9453←↵7.52367 78.3662 4.91014←↵3.41255 78.3575 8.91427←↵1.34585 78.3559 10.892←↵'} {'-2.24836 78.0867 -13.3274←↵-4.30489 78.1857 -11.3366←↵-6.35851 78.1949 -9.33247←↵-2.33861 78.2356 -9.27153←↵-0.282047 78.1931 -11.2637←↵-8.38568 78.1782 -7.3586←↵-10.4495 78.2449 -5.35992←↵-4.36473 78.26 -7.30336←↵-6.43174 78.2882 -5.34185←↵-2.38286 78.2892 -5.25741←↵-0.319205 78.2588 -7.24715←↵-12.518 78.237 -3.36747←↵-8.46859 78.2766 -3.342←↵-10.4874 78.309 -1.32669←↵-4.42502 78.2621 -3.26711←↵-6.49969 78.307 -1.25888←↵-2.4606 78.2805 -1.19602←↵-0.396895 78.2955 -3.19541←↵-12.5409 78.2902 0.649523←↵-8.54126 78.2945 0.729485←↵-10.5624 78.2892 2.70351←↵-4.53972 78.3022 0.784238←↵-6.55744 78.309 2.79984←↵-0.480238 78.2615 0.851321←↵-2.50849 78.3095 2.83227←↵-8.58676 78.3067 4.78649←↵-4.57823 78.3116 4.81308←↵-6.60987 78.3123 6.80625←↵-0.510959 78.2957 4.88307←↵-2.5859 78.313 6.87972←↵-4.61351 78.3096 8.86284←↵-2.62027 78.3138 10.9062←↵-0.586243 78.3165 8.9013←↵1.77456 78.088 -13.2322←↵1.76603 78.2709 -9.17288←↵3.77015 78.1572 -11.2172←↵5.74214 78.226 -9.16817←↵1.66299 78.2977 -5.18456←↵3.7281 78.3143 -7.1313←↵5.71625 78.283 -5.12472←↵7.72931 78.2548 -7.0936←↵9.71575 78.2679 -5.0449←↵1.58088 78.2931 -1.12804←↵3.64845 78.2984 -3.12699←↵5.62647 78.293 -1.04887←↵7.66382 78.306 -3.04398←↵9.63981 78.2998 -0.982748←↵11.6979 78.3171 -2.94339←↵1.53013 78.3162 2.87819←↵3.58873 78.2883 0.939327←↵5.55402 78.2989 3.02911←↵7.59452 78.3123 1.01945←↵9.58041 78.3109 3.03976←↵11.6139 78.2999 1.05795←↵1.46464 78.3174 6.94397←↵3.50808 78.3033 4.98336←↵5.50437 78.3084 7.01705←↵7.54299 78.3106 5.05212←↵3.4555 78.3079 8.97317←↵1.37627 78.3076 10.9376←↵'} {'-2.18107 78.0563 -13.1644←↵-4.23615 78.1278 -11.2371←↵-6.29896 78.1242 -9.21386←↵-2.24988 78.1582 -9.17176←↵-0.216913 78.1309 -11.1316←↵-8.35862 78.1452 -7.22726←↵-10.4017 78.1645 -5.23704←↵-4.25771 78.1942 -7.16492←↵-6.30702 78.1933 -5.1922←↵-2.30337 78.2213 -5.06537←↵-0.260031 78.2534 -7.09287←↵-12.4449 78.1888 -3.22057←↵-8.36125 78.2447 -3.2157←↵-10.4439 78.2572 -1.20345←↵-4.3398 78.2179 -3.09224←↵-6.38447 78.2575 -1.09308←↵-2.3443 78.2362 -1.06067←↵-0.32332 78.2543 -3.03254←↵-12.4835 78.2626 0.754261←↵-8.46192 78.2318 0.861508←↵-10.508 78.2428 2.85021←↵-4.38264 78.2604 0.942466←↵-6.46953 78.267 2.89314←↵-0.381502 78.2617 0.985675←↵-2.42641 78.2367 2.97067←↵-8.51688 78.2576 4.90017←↵-4.4907 78.269 4.98375←↵-6.55239 78.2506 6.96534←↵-0.451074 78.269 5.01371←↵-2.49103 78.2498 7.04078←↵-4.5566 78.2548 9.0192←↵-2.54357 78.2627 11.0249←↵-0.533596 78.2665 9.05947←↵1.81501 78.1353 -13.0927←↵1.79015 78.2065 -9.10339←↵3.82002 78.1823 -11.0658←↵5.82846 78.2401 -9.00854←↵1.74528 78.2454 -5.01504←↵3.76557 78.2405 -7.03656←↵5.76887 78.2476 -4.97309←↵7.80985 78.2476 -6.94503←↵9.80877 78.2294 -4.92233←↵1.68211 78.2512 -0.988379←↵3.72781 78.2423 -2.98762←↵5.69996 78.2474 -0.920884←↵7.74281 78.24 -2.89706←↵9.73103 78.2476 -0.872117←↵11.7759 78.2574 -2.83472←↵1.57091 78.2603 3.02124←↵3.60538 78.2355 1.04595←↵5.62468 78.2599 3.1089←↵7.71017 78.2288 1.1236←↵9.6823 78.2384 3.15968←↵11.7137 78.2321 1.14823←↵1.50731 78.2514 7.06637←↵3.57706 78.2631 5.11035←↵5.50688 78.2623 7.11638←↵7.60712 78.2545 5.14635←↵3.48068 78.2629 9.11938←↵1.45661 78.2672 11.095←↵'}
N = numel(C);
fprintf('%d blocks found.\n',N);
6 blocks found.
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)
V = 1×6
12.5130 12.5012 12.5117 12.5076 12.4850 12.4845
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)

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!

Translated by