Why does my data not show up?
7 views (last 30 days)
Show older comments
Hi. I am trying to write a code that displays shapefiles with data on it on a basemap and have a set of grid boxes that go over the shapefile (benthic) and count the amount of data points in each box. Most of it works, however the boxes don't cover the entire benthic region and it says that there are no data points in the boxes when there are. I attached a snippet of code that is supposed to do just those things but it's not. Is there something that I'm missing that is the reason why it's not working?? Any help or tips would be much appreciated!! Thank you.
Code:
% Initialize variables to store class counts in each grid cell
numLatCells = length(latGrid) - 1;
numLonCells = length(lonGrid) - 1;
classCountsInGridCell = cell(numLatCells, numLonCells);
% Calculate the actual cell size based on the new number of grid cells
actualCellSize = 1 / (111.32 * 1000); % 1 degree of latitude is approximately 111.32 km or 111,320 meters
cellSizeLat = (latLimits(2) - latLimits(1)) / numLatCells;
cellSizeLon = (lonLimits(2) - lonLimits(1)) / numLonCells;
% Create a matrix to store grid cell information
gridMatrix = zeros(numLatCells, numLonCells, 'int16');
% Create a grid of latitude and longitude values covering the entire map extent
latGrid = linspace(latLimits(1), latLimits(2), numLatCells + 1);
lonGrid = linspace(lonLimits(1), lonLimits(2), numLonCells + 1);
% Repeat over each cell in the grid and store class attributes if within the benthic area
for i = 1:numLatCells
for j = 1:numLonCells
% Find the data points within the current grid cell
inGridCell = lat >= latGrid(i) & lat < latGrid(i+1) & lon >= lonGrid(j) & lon < lonGrid(j+1);
% Check if any data points fall within the benthic shapefile
if sum(inGridCell) > 0
gridMatrix(i, j) = 1;
end
end
end
% Plot the boxes on the map
hold on;
gridCounter = 1; % Initialize grid counter
for i = 1:numLatCells
for j = 1:numLonCells
if gridMatrix(i, j) ~= 0
% Define the grid cell corners as a polygon
latPolygon = [latGrid(i), latGrid(i+1), latGrid(i+1), latGrid(i), latGrid(i)];
lonPolygon = [lonGrid(j), lonGrid(j), lonGrid(j+1), lonGrid(j+1), lonGrid(j)];
% Plot the grid cell as a polygon without adding to the legend
geoplot(ax, latPolygon, lonPolygon, 'Color', 'k', 'LineWidth', 1, 'HandleVisibility', 'off');
% Get the center of the current grid cell
boxCenterLat = (latGrid(i) + latGrid(i+1)) / 2;
boxCenterLon = (lonGrid(j) + lonGrid(j+1)) / 2;
% Get the distance from the box center to Pinatubo's summit
distanceToSummit = deg2km(distance(summitLat, summitLon, boxCenterLat, boxCenterLon));
% Display the distance for the current grid cell
text(boxCenterLat, boxCenterLon, sprintf('%.2f km', distanceToSummit), 'Color', 'red', 'HorizontalAlignment', 'center');
% Check if the grid cell is within the benthic area
if ~isempty(classAttributesInGridCell{i, j})
% Get the class attributes for the current grid cell
cellClassAttribute = classAttributesInGridCell{i, j};
% Count the unique classes and their occurrences in the current grid cell
[cellUniqueClasses, cellClassCounts] = uniqueCount(cellClassAttribute);
% Display the result for the current grid cell
fprintf('Grid %d:\n', gridCounter);
fprintf('Class Counts:\n');
for k = 1:numel(uniqueClasses)
classIndex = find(strcmp(cellUniqueClasses, uniqueClasses(k)));
if isempty(classIndex)
fprintf('%s: %d\n', uniqueClasses{k}, 0);
else
fprintf('%s: %d\n', uniqueClasses{k}, cellClassCounts(classIndex));
end
end
fprintf('\n');
else
fprintf('Grid %d:\n', gridCounter);
fprintf('No data points in this grid cell.\n\n');
end
% Increment grid counter
gridCounter = gridCounter + 1;
end
end
end
hold off;
1 Comment
Walter Roberson
on 28 Jul 2023
We do not know anything about classAttributesInGridCell .
We could hypothesize that maybe it is the transpose of what you are expecting -- that maybe its first index is long rather than lat.
Note: you should consider optimizing your code. There are a series of calculations that do not need to be done if classAttributesInGridCell{i,j} is empty.
Also, instead of your loop building gridMatrix you could probably use histogram2 to get the counts per bin and then gridMatrix would just be (per_bin_count > 0) which could be further optimized to gridMatrix = logical(per_bin_count)
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!