MATLAB Answers

Use inpolygon command for multiple polygon areas

5 views (last 30 days)
Tessa Kol
Tessa Kol on 18 Sep 2020
Commented: Tessa Kol on 19 Sep 2020
Dear all,
I have some difficulty with assigning points into multiple square polygons. I have data files containing (x,y,z) coordinates of every particle. With the command inpolygon I locate the particles that are inside a specified polygon:
%% Loading the data
rhoPart = 2540;
files = dir(fullfile(uigetdir,'*.data*'));
[~,Index] = natsort({});
files = files(Index);
expData = cell(length(files),1);
k = 1;
for i = 1:length(files)
fid = fopen(fullfile(files(i).folder,files(i).name),'r');
%% Reading the data
% Read all the data from the file
dataRead = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f','HeaderLines',1);
% Write headerline N, time, xmin, ymin, zmin, xmax, ymax, zmax
runData{k} = strsplit(fgetl(fid), ' ');
% Write only the x, y, and z components of the particles, particle radius,
% z component+ particle radius and volume of the particle
expData{k} = [dataRead{1}(:,1) dataRead{2}(:,1) dataRead{3}(:,1) dataRead{7}(:,1) dataRead{3}(:,1)+dataRead{7}(:,1) rhoPart*(4/3)*pi*(dataRead{7}(:,1).^3)];
% Write only the vx,vy,vz of the particles and magnitude
velData{k} = [dataRead{4}(:,1) dataRead{5}(:,1) dataRead{6}(:,1) sqrt(dataRead{4}(:,1).^2 + dataRead{5}(:,1).^2 + dataRead{6}(:,1).^2)];
k = k + 1;
%% Classify (into a structure)
% Number of simulation runs
N_run = 1;
% Number of .data files to be extracted of every simulation run
N_files = 2745;
k = 1;
for i = 1:N_run
for j = 1:N_files
runData_r{j,i} = str2double(runData{k});
expData_r{j,i} = expData{k};
velData_r{j,i} = velData{k};
k = k + 1;
% Polygon
xc = expData_r{1800,1}(:,1);
zc = expData_r{1800,1}(:,1);
xv = [-0.0184 -0.0061 -0.0061 -0.0184 -0.0184];
zv = [0.2857 0.2857 0.2755 0.2755 0.2857];
in = inpolygon(xc,zc,xv,zv);
hold on
I get the following results:
But I want to do this procedure for a whole mesgrid. Thus, locate the particles of every gridbin (i.e. multiple polygons). How can I do that?


Sign in to comment.

Accepted Answer

KSSV on 18 Sep 2020


Show 5 older comments
Tessa Kol
Tessa Kol on 18 Sep 2020
Yes, I check if the indexing went correctly and it seems fine to me. So now I have to do indeed with mean. I worked with the code below:
vz_mean = cellfun(@(x)mean(x(:,2)),iwant2);
vx_mean = cellfun(@(x)mean(x(:,1)),iwant2);
However, I can still not use quiver since X and Z do not match with vz_mean and vx mean. Because in the for loop iwant2 is calculated for 49 x 49 bins and X and Z are 50 x 50 bins. How can I resolve this?
KSSV on 18 Sep 2020
Why do you think the indices are wrong?
Tessa Kol
Tessa Kol on 19 Sep 2020
Everything is solved by now. I succesfully made a quiver plot. Thank you for you help!

Sign in to comment.

More Answers (1)

Matt J
Matt J on 18 Sep 2020
Edited: Matt J on 18 Sep 2020
Use discretize(),
Apply it separately to all of your xv's and then on all your zv's.


Sign in to comment.




Community Treasure Hunt

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

Start Hunting!