Apply my calculation for all data file

1 次查看(过去 30 天)
Tesla
Tesla 2021-7-16
评论: Tesla 2021-7-17
I wrote this code to calculate the mass centre and save it, taking X and Y cordinate from multiple files.
But my code is working just with the first colum on those file, while in my files I have multiple column for X and Y coordinate.
I want to loop my code for all culumns.
function [x_cen, y_cen] = polygonCentroid(x,y)
clc;
fileID = fopen('Centre_new.dat','wt');
myFolder = 'C:\Users\asus\Desktop\Lingr\Bif10';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
end
filePattern = fullfile(myFolder, 'shape_*.dat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
S = dir(fullfile(myFolder,'shape_0*.dat'));
N = natsortfiles({S.name}); % sort the filenames!
for k = 1:numel(N)
baseFileName = N{k};
%for k = 1 : length(theFiles)
% baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data{k}=dlmread(fullFileName);
x=data{k}(:,1);
y=data{k}(:,2);
xm = mean(x);
ym = mean(y);
x = x - xm;
y = y - ym;
% summations for CCW boundary
xp = x( [2:end 1] );
yp = y( [2:end 1] );
a = x.*yp - xp.*y;
l=t*k;
A{k} = sum( a ) /2;
xc{k} = sum( (x+xp).*a ) /6/A{k};
yc{k} = sum( (y+yp).*a ) /6/A{k};
% replace mean of vertices
x_cen{k} = xc{k} + xm;
y_cen{k} = yc{k} + ym;
fprintf(fileID,'\n%f %f%f\n',l, y_cen{k});
end
fclose(fileID);
return
My input files are like that: X1 Y1 X2 Y2....
And my code do the calculation just for the first particle X1 Y1
26382 199.31 25673 196.96 25769 218.53
26383 198.99 25674 196.71 25769 218.54
26383 198.63 25675 196.46 25770 218.58
26384 198.28 25675 196.21 25771 218.66
26384 197.92 25676 195.97 25771 218.78
26385 197.58 25676 195.73 25772 218.94

回答(1 个)

Simon Chan
Simon Chan 2021-7-16
编辑:Simon Chan 2021-7-16
Add a for loop for each file as follows:
for k = 1:numel(N)
baseFileName = N{k};
%for k = 1 : length(theFiles)
% baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
data{k}=dlmread(fullFileName);
for s = 1:size(data{k},2)/2
x=data{k}(:,2*s-1);
y=data{k}(:,2*s);
xm = mean(x);
ym = mean(y);
x = x - xm;
y = y - ym;
% summations for CCW boundary
xp = x( [2:end 1] );
yp = y( [2:end 1] );
a = x.*yp - xp.*y;
l=t*k;
A{k,s} = sum( a ) /2;
xc{k,s} = sum( (x+xp).*a ) /6/A{k,s};
yc{k,s} = sum( (y+yp).*a ) /6/A{k,s};
% replace mean of vertices
x_cen{k,s} = xc{k,s} + xm;
y_cen{k,s} = yc{k,s} + ym;
fprintf(fileID,'\n%f %d %f %f\n',l,s, x_cen{k,s}, y_cen{k,s});
end
end
fclose(fileID);
return
  13 个评论
Simon Chan
Simon Chan 2021-7-17
编辑:Simon Chan 2021-7-17
If I define t=1 and I run the code from the following line and extract the data from the files.
filePattern = fullfile(myFolder, 'shape_*.dat'); % Change to whatever pattern you need.
Since I don't have the function 'natsortfiles', I sort the file names on my own way and got the following results: (Noticed that t = 1, otherwise you get empty output.
Now reading C:\SIMON\z\shape_01.dat
1.000000 192.226381 269.773132 261.632040 232.669130 251.291122 227.502570 176.063843 277.957061 198.980804 259.257445 198.140514 239.750078 213.285716 265.022210 237.798327 186.553294 228.719952 214.371639 203.873247 253.465992 253.408220 200.041573 243.389241 243.723523 198.631092 189.608892 224.515031 211.203193 253.069030 243.555460 223.385546 191.857669 181.321631 216.154546 186.627176 202.755613 190.467401 184.729421 183.463581 243.295744 240.884320 187.742119 184.263593 225.238160 190.853960 211.212454 240.790122 206.912395 209.289726 195.517161 244.727633 210.117472 248.969161 233.174819 229.512688 188.165967 219.731152 222.225537 170.283334 249.284295 228.922954 247.228086 176.351504 248.074574 181.800139 164.332989 183.419035 206.748522 231.961466 241.063735 212.283058 238.199392 184.502566 235.001903 180.612327 242.460453 193.552812 175.208181 262.975664 186.879834 238.465722 208.413927 205.869042 237.725249 185.099979 215.105776 247.998525 196.646864 227.518164 163.836840
Now reading C:\SIMON\z\shape_02.dat
2.000000 192.334150 272.737841 259.015189 234.372871 251.279307 226.581834 180.302381 274.508429 197.220076 264.954122 196.880666 239.302312 216.568298 265.845102 238.154127 187.504236 226.835034 213.214479 207.205717 253.254085 253.142106 198.648880 245.135704 245.551084 194.305756 189.763409 223.906999 210.002047 252.817931 243.221089 224.786318 191.345762 184.035394 214.333643 186.705209 203.509885 190.449909 183.835763 185.051024 245.560526 240.763886 187.877168 184.379022 225.045234 189.290232 214.330358 238.823500 205.780870 210.413183 192.797343 246.109966 209.163495 248.054226 232.369365 228.573363 188.577304 220.351635 220.428155 167.863681 246.698981 234.250664 243.923843 172.825917 242.033758 177.695602 164.061685 186.643519 208.716646 229.691901 245.478079 211.129248 239.339706 185.294912 233.682636 180.740226 243.152192 193.637758 170.966215 257.976717 186.668422 234.260780 214.207628 206.339148 236.980180 184.821954 214.269289 248.596614 193.375862 229.397970 168.582822
Now reading C:\SIMON\z\shape_03.dat
3.000000 191.439921 274.497453 256.569854 235.962552 251.256113 225.531732 183.696286 272.159102 196.516545 267.915651 193.874481 240.115451 218.708407 263.787573 237.758480 187.888358 225.204883 211.906246 210.022734 252.817551 252.408370 198.217034 245.305366 246.713594 192.179948 189.274725 224.409452 209.468970 252.639482 243.648903 225.558987 192.149247 186.850347 214.056975 186.716537 203.531457 190.002402 183.812803 185.995281 246.240418 240.550482 187.566623 184.921129 224.055406 187.092920 216.371535 237.434305 204.778442 211.291995 190.822746 250.617051 208.190222 247.250291 231.172218 228.156879 188.751927 222.097350 219.398468 167.685585 245.232192 235.313638 240.315592 170.287485 239.338447 172.924737 166.256467 189.087482 209.441647 228.124494 247.943314 208.038870 240.320844 186.161360 233.493750 183.440003 244.182331 193.687691 168.114954 255.515841 186.554860 232.136781 216.963434 206.748625 236.633760 185.445263 214.721330 248.527551 190.301809 233.759033 172.551495
Now reading C:\SIMON\z\shape_04.dat
4.000000 191.123962 275.919127 253.782843 237.726819 251.031358 224.854446 187.147789 269.877818 196.194603 269.611903 189.275557 241.361555 220.460924 260.703317 237.264875 188.023758 223.578632 211.624677 211.223717 252.227057 251.609114 198.592029 245.035945 247.523218 190.247166 188.842956 225.223968 210.286523 252.406375 243.639768 225.784347 193.226788 189.670835 214.573346 186.912936 203.397729 189.364702 183.796695 186.384041 246.149056 239.515822 187.066528 185.718544 222.695494 184.903905 217.715340 235.947584 204.045161 211.690650 188.396289 255.461438 207.351193 246.926645 229.196215 227.721865 188.336360 222.881050 218.389549 169.965199 244.871895 235.354367 236.630428 169.488301 238.838747 168.160110 169.199605 191.681805 209.921563 225.825304 250.228472 204.732933 241.791846 187.000094 232.758149 186.682115 244.683200 193.032532 165.461384 253.620531 186.446390 232.018160 217.957332 206.290265 237.786399 186.358664 215.770765 248.819792 187.004623 239.560079 176.403037
Now reading C:\SIMON\z\shape_05.dat
5.000000 190.858898 276.985160 250.736774 238.791309 250.828218 225.524429 190.782107 267.050388 195.889888 269.691470 184.527726 242.350794 222.329012 257.476014 237.157506 188.074239 221.332915 211.235983 211.117365 251.594919 250.853450 199.563880 244.835381 247.895787 188.013121 188.721209 225.656008 211.276615 252.010579 243.023021 225.507173 194.961811 191.907772 215.468762 187.281820 203.409265 189.219458 183.705441 186.196241 246.778059 237.566553 186.611103 186.612077 222.270267 183.928173 219.140214 234.235899 204.264917 211.421022 185.465059 258.697357 206.193921 246.608630 227.575124 227.412698 187.678863 225.104209 216.604410 173.453696 244.843562 237.294409 232.464439 171.967091 239.245859 164.584158 172.015103 194.378755 210.164295 222.943491 253.336749 202.845453 243.065435 187.344936 231.104279 189.664694 244.514388 191.853126 162.721127 250.961540 186.652286 231.653114 219.167078 205.753019 239.549365 186.985381 216.598352 249.340948 184.175070 242.895241 180.178240
Now reading C:\SIMON\z\shape_06.dat
6.000000 190.853772 277.924754 246.754059 239.353849 250.816359 226.445578 194.372912 263.565082 195.235932 267.842047 179.979684 242.907014 225.242106 254.661754 237.237931 187.801765 218.675098 211.384991 211.397753 250.964874 250.060150 200.767323 244.638041 247.774099 185.465318 188.820024 226.053220 212.123992 251.505955 242.443406 224.894303 197.437042 193.090502 216.370978 187.639773 203.031165 189.508179 183.883156 185.770552 247.644411 235.700737 186.344976 187.346297 222.593997 184.307164 220.677273 232.282649 204.465021 211.126932 182.289816 261.898917 205.181853 246.073590 226.384035 227.617774 187.554829 229.202783 214.988374 176.789223 244.732371 240.066911 227.402235 176.242683 240.057303 164.048848 174.807626 197.407302 210.322683 220.725805 256.754025 202.101144 243.761291 185.911648 229.449933 192.418849 243.628977 190.815601 160.073120 246.821407 186.831166 231.644426 220.179657 205.338344 240.974362 187.413421 217.063670 249.862319 183.553297 245.752846 184.147440
Now reading C:\SIMON\z\shape_07.dat
7.000000 190.912434 277.281141 243.525902 239.538276 250.691462 227.450721 196.596842 259.782717 194.051728 265.001445 177.330302 243.115301 230.483527 252.059090 238.028151 187.301899 215.898832 210.667321 212.698439 250.581863 249.186927 202.051112 244.467307 247.274637 182.478431 187.906334 226.461357 212.203176 250.953674 242.039807 224.052849 199.401670 193.463701 217.164816 187.974987 202.363041 189.652405 184.130055 185.403246 249.280959 234.918344 186.279372 187.913384 224.882736 181.949747 222.243121 229.864069 204.683205 210.784897 180.570163 265.069354 204.757500 245.570315 225.995081 228.414982 187.972820 231.856419 213.854368 180.347131 244.387903 243.262507 224.164410 181.368644 241.131107 166.678819 178.322334 201.953343 210.092709 220.184998 259.386894 199.138545 244.392523 183.216862 228.221930 195.617817 242.269407 189.863008 158.564479 241.289819 186.965251 232.098104 220.361698 204.001423 242.393218 187.548353 217.906883 250.438574 185.791852 248.045891 188.653495
Now reading C:\SIMON\z\shape_08.dat
8.000000 191.171742 274.600600 241.496940 239.778580 250.194026 227.942045 197.373312 255.806276 192.801158 261.661956 175.011030 243.002919 235.076490 249.390389 240.263745 186.791505 213.961464 208.461896 214.176138 250.388075 248.240042 203.145835 244.567277 246.931018 181.135245 184.881676 226.921187 211.711093 250.549943 241.861169 223.235185 199.634499 193.250706 218.025519 188.358268 201.675238 189.431098 184.099711 185.293509 252.180874 234.416008 186.495281 188.332315 228.763888 178.151852 222.971225 227.042788 204.727043 210.309913 183.192696 268.183449 205.293844 245.307333 226.195055 229.933901 188.496837 234.324028 213.903364 184.075354 243.874118 246.744972 224.027332 186.345241 242.071708 170.237301 182.615494 206.986196 210.154269 219.961452 258.582983 195.616613 245.054306 180.028971 227.960377 199.664569 240.580836 189.003428 160.316462 236.479325 186.967814 232.247635 219.382835 201.039451 243.628885 187.711307 219.927310 250.989669 188.969556 249.713956 192.691409
Now reading C:\SIMON\z\shape_09.dat
9.000000 190.998227 271.016191 240.482996 240.264863 249.401371 227.674171 197.933359 252.049735 192.584208 258.103911 172.119688 242.359099 237.952907 246.406841 243.517986 186.408851 212.940641 205.188405 215.447731 250.245030 247.479027 203.595017 244.663269 246.346875 183.918651 180.538950 227.249945 211.455510 250.489649 241.864976 222.379365 200.663527 192.892875 218.077703 188.640537 201.201257 189.572079 184.330646 185.500922 255.769097 233.515037 186.869147 188.609407 233.153631 175.447131 224.009052 224.631749 204.574306 209.379434 187.222403 271.199068 205.215147 245.472669 227.017858 232.997744 188.896330 237.300358 213.389567 188.746629 243.246455 250.719482 225.122490 191.345406 242.909965 174.229655 186.848525 209.819557 210.753856 218.944483 255.283907 191.396365 245.603344 176.539389 227.270431 204.307065 238.210165 188.204600 164.201566 234.734003 186.830910 231.842136 218.861688 195.812772 244.578379 188.391579 221.605334 251.537755 192.399528 251.682207 195.084296
Now reading C:\SIMON\z\shape_010.dat
10.000000 190.443212 267.191622 240.162485 241.192145 248.741481 227.086526 198.296143 249.435926 191.490801 254.158864 171.266741 241.761443 241.448298 244.142125 246.722240 186.117139 212.667754 202.274852 216.342749 250.165225 246.607781 203.664709 245.170920 246.003185 188.219069 176.345117 227.417793 210.746538 251.432831 242.097336 221.546834 202.058356 192.751496 217.870977 188.859274 200.801140 189.620266 184.686620 185.773790 259.231887 232.826355 187.137991 188.940651 235.324883 177.074946 224.298123 224.296495 204.492863 208.405989 191.091961 273.830490 205.291263 245.882923 227.673580 238.330092 189.195615 240.596483 213.632488 192.505690 242.625906 253.688782 226.560802 197.881876 243.546203 178.440603 189.307451 211.335208 211.386581 217.805257 251.376882 186.972089 245.262061 172.558630 227.347189 206.358753 234.822609 187.643152 168.568522 234.775767 186.643296 230.615324 218.279468 191.298646 245.437203 189.270023 223.648600 251.935845 196.171504 253.951034 196.222346
Tesla
Tesla 2021-7-17
Thank you for the suggestion, but still not working. (Yes i have t).
What about storing every l and y_cen in different file, instead of saving all in one file. it could be easier.
which means I will have 90 data file genereted.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by