How to plot a large number of rectangles from a matrix
4 views (last 30 days)
Show older comments
I have thousands of files of survey data. My ultimate goal is find a way of grouping these together by geographical region and concatenating those that are similar. This is a difficult concept for me but what Ive decided might be the easiest is to first find there geographical limits, graph a rectangle of each using the NE and SW corner, find the area, then determine if the surveys are in the same region using the amount of overlap from each rectangle. For instance, if the area of one survey rectanlge over laps another by 70 percent then they could be considered the same region and the files should be concatenated before being processed. Hopefully that makes sense and if there is an easier way of doing this please let me know.
To start im trying to create these rectangles. The first thing I did was set up where the files are and how they should be read. Next I found the max and min lat and long. I created four points, minlong maxlat, minlong minlat, maxlon minlat, maxlon maxlat. This should be enough to create a rectangle right? The first issue im running into is how to get every file to add these four values to one matrix, Im getting confused on the format of it all, how should the matrix be setup to be read as ordered points and individual rectangles? Right now I have two columns with 12 total rows from 3 files, everything was read correctly but how I do then plot each of these as individual rectangles to find the area? Im pretty lost but the little bit of code Ive created is below. Thanks for any and all help!
clear
P = 'C:/Users/lastname/OneDrive/Desktop/Single Beam Bathy/SN06222';
Q = 'C:/Users/lastname/OneDrive/Desktop/Single Beam Bathy/SN06222/Corrected CSV';
S = dir(fullfile(P,'*.csv'));
% S = natsortfiles(S);
N = numel(S);
C = cell(N,2);
for k= 1:numel(S)
F1 = fullfile(S(k).folder,S(k).name);
M = readmatrix(F1);
lat = M(:,1);
lon = M(:,2);
[minLat, maxLat] = bounds(lat);
[minLon, maxLon] = bounds(lon);
C{k,1} = [maxLat; maxLat; minLat; minLat];
C{k,2} = [maxLon; minLon; maxLon; minLon];
c = cell2mat(C);
end
0 Comments
Accepted Answer
Matt J
on 19 Mar 2024
Edited: Matt J
on 19 Mar 2024
I don't really see why you need to find bounding rectangles to do what you describe. You could just represent the regions as polyshapes,
N=numel(S);
p(N)=polyshape();
for k= 1:numel(S)
F1 = fullfile(S(k).folder,S(k).name);
p(k) = polyshape(readmatrix(F1));
end
plot(p)
You can also find the overlap area of two of the regions using polyshape methods,
overlap(j,k) = area( intersect( p(j),p(k) ) )
4 Comments
Matt J
on 20 Mar 2024
The polyshape colors and alphas are under your control through the handles returned by plot(), e.g.,
p1 = polyshape([0 0 1 1],[1 0 0 1]);
p2 = polyshape([0.75 1.25 1.25 0.75],[0.25 0.25 0.75 0.75]);
p3 = polyshape([1.25 1.25 1.75 1.75],[0.75 1.25 1.25 0.75]);
polyvec = [p1 p2 p3];
H=plot(polyvec);
H(1).FaceColor='r'; H(1).FaceAlpha=0.8;
H(2).FaceColor='g'; H(2).FaceAlpha=0.2;
H(3).FaceColor='b';
More Answers (0)
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!