How do I extract points from multiple circles?
1 view (last 30 days)
Show older comments
I have plot circle according to the grid points and I have a problem to extract the points within each circle. This is my coding..
How should I extract the points from each circle. and save it in separate files according to the grid points. anyone can help?
clc;clear;
% Number of Points
numPoints = 5475;
data = load('Sample.txt');
x = data (:,2);
y = data (:,1);
z = data (:,3);%
plot(x, y, '.r');
% Make circle.
grid = load('Grid025.asc');
[row,column]=size(grid);
x0 = grid (:,2);
y0 = grid (:,1);
R = 0.09;
% Plot circle
for i=1:row
pos = [x0(i)-R, y0(i)-R, 2*R, 2*R];
rectangle('Position', pos, 'Curvature',[1 1]);
hold on;
% Determine how many points are in the circle .
count(i) = sum(((x-x0(i)).^2+(y-y0(i)).^2<=R^2));
listC = count.';
%Extract Data in Each Circle
distances = sqrt((x-x0(i)).^ 2 + (y-y0(i)).^ 2);
InsideCircle = distances <=R;
xS = x(InsideCircle); % Selected Lon
yS = y(InsideCircle); % Selected Lat
zS = z(InsideCircle); % Selected h
end
0 Comments
Accepted Answer
Cedric
on 27 Apr 2019
Edited: Cedric
on 27 Apr 2019
We cannot test without your data, but the approach seems fine. You could use the PDIST2 function and extract everything in one shot, but as a first approach this is fine.
I will assume that the question is about saving the points, so here is small update of your code that should (if I understand your example well) generate an Excel file with 4 columns that store Circle ID, Point ID, Point X, Point Y, Point Z:
% Prealloc array of counts, and cell array for storing relevant point parameters per circle.
counts = zeros( row, 1 ) ;
output = cell( row, 1 ) ;
% Initialize figure.
figure() ;
hold on ;
% Process circles.
for circleId = 1 : row
% Plot circle.
pos = [x0(circleId)-R, y0(circleId)-R, 2*R, 2*R] ;
rectangle( 'Position', pos, 'Curvature', [1 1] ) ;
% Compute distance from all points to current circle center.
distances = sqrt( (x - x0(circleId)).^2 + (y - y0(circleId)).^2 ) ;
% Flag and count relevant points.
isInsideCircle = distances <= R ;
counts(circleId) = sum( isInsideCircle ) ;
% Extract relevant parameters.
pointsId = find( isInsideCircle ) ;
pointsX = x(isInsideCircle) ; % Selected Lon
pointsY = y(isInsideCircle) ; % Selected Lat
pointsZ = z(isInsideCircle) ; % Selected h
% Store in relevant output cell. We build the first column as a vector of elements equal
% to circleId, whose size matches the size of the other columns (e.g. pointsId).
output{circleId} = [circleId * ones( size( pointsId )), pointsId, pointsX, pointsY, pointsZ] ;
end
% Build data to export to Excel by concatenating all cells of the output cell array vertically
% into a large numeric array, converting it into a cell array, and concatenating to a first
% "header" row.
output = num2cell( vertcat( output{:} )) ;
output = [{'CircleID', 'PointID', 'PointX', 'PointY', 'PointZ'}; output] ;
% Export to Excel.
xlswrite( 'PointsPerCircle.xlsx', output ) ;
Note that I didn't test it, but it illustrates a possible approach. Let me know if you have any question.
9 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!