After loading F_points, select one of the points by indicating its row index in the variable "pt". This solution computes the euclidean distance between that target point and all other points and then selects the 10 closest points. It then plots the results.
The index of all coordinates in order of proximity to the target point is stored in ascendIdx. The coordinates for the closest 10 points is stored in xyNearest.
xy = cell2mat(F_points)';
pt = 20;
dist = sqrt((xy(:,1)-xy(pt,1)).^2 + (xy(:,2)-xy(pt,2)).^2);
n = 10;
[~, ascendIdx] = sort(dist);
ascendIdx(ascendIdx==pt) = [];
xyNearest = xy(ascendIdx(1:n),:);
figure()
plot(xy(:,1),xy(:,2),'bo')
hold on
plot(xy(pt,1),xy(pt,2),'bo','MarkerFaceColor', 'y')
plot(xyNearest(:,1),xyNearest(:,2),'r+')
plot([xy(pt,1)*ones(n,1), xyNearest(:,1)]',...
[xy(pt,2)*ones(n,1), xyNearest(:,2)]', 'k-')
Results when pt = 20. The yellow dot is the one selected, the red + indicate the 10 closest neighbors.
[update]
To apply that for every point rather than just for one selected point, dist(n,m) below is a square matrix of the distance between point xy(n,:) and xy(m,:). ascendIdx is a square matrix of index values for each coordinate, sorted by closest distances. So, ascendIdx(p,q) is row index of q_th furthest point from xy(p,:).
xy = cell2mat(F_points)';
dist = cell2mat(arrayfun(@(i)sqrt((xy(:,1)-xy(i,1)).^2 + (xy(:,2)-xy(i,2)).^2), 1:size(xy,1), 'UniformOutput', false));
n = 10;
[~, ascendIdx] = sort(dist);
See comments below for a comparison between this method and knnsearch() which is much slower.
Best Answer