MATLAB Answers

Apply my calculation for all data file

1 view (last 30 days)
Tesla
Tesla on 16 Jul 2021
Commented: Tesla on 17 Jul 2021
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

Answers (1)

Simon Chan
Simon Chan on 16 Jul 2021
Edited: Simon Chan on 16 Jul 2021
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 Comments
Tesla
Tesla on 17 Jul 2021
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by