Filter rows and columns with datas (find)
1 view (last 30 days)
Show older comments
Augusto Gabriel da Costa Pereira
on 3 Jan 2024
Commented: Sulaymon Eshkabilov
on 3 Jan 2024
I'm performing longitude and latitude filters to effectively filter data from the file data_mat_h12v09_2010_195.mat:
'LatIndex' is all right with 16 identified latitudes. However, 'LonIndex' isn't entirely accurate; it's underestimating the number of longitudes it should find. It should be around 16 but is finding 121 instead. This can be verified in 'combined_filter = lat_filter & lon_filter;' and in the '% Plotting' section (https://drive.google.com/drive/folders/18GgPs-iN3brZHQ3pZNK8GyLt4SOmemY8?usp=drive_link).
The files 'load('Lat_and_Lon.mat')' are available on the Google Drive link due to their size of 10 MB.
clc; clear all
cd '/media/augusto/TOSHIBA EXT/output_modis_maiac/output_modis_maiac_h12v09'
%%
dir_path = '/media/augusto/TOSHIBA EXT/output_modis_maiac/output_modis_maiac_h12v09';
variable_name ='data';
data_aod = [];
load('Lat_and_Lon.mat')
file_list = dir(fullfile(dir_path, 'data_mat_h12v09_2010_195.mat'));
%%
% Define your latitude and longitude boundaries
lat_bounds = [-2.9580, -2.8231]; % Latitude interval
lon_bounds = [-60.0372, -59.9023]; % Longitude interval
% Filter for points within the specified latitude and longitude intervals
lat_filter = (lat_h12v09 >= lat_bounds(1)) & (lat_h12v09 <= lat_bounds(2));
lon_filter = (lon_h12v09 >= lon_bounds(1)) & (lon_h12v09 <= lon_bounds(2));
% plots para visualizar se a seleçao esta ok
% figure,pcolor(lon_h12v09,lat_h12v09,double(lon_filter)),shading flat
% figure,pcolor(lon_h12v09,lat_h12v09,double(lat_filter)),shading flat
% Create a combined filter for both latitude and longitude
combined_filter = lat_filter & lon_filter;
% plot para visualizar se a o combinado esta ok
% figure,pcolor(lon_h12v09,lat_h12v09,double(combined_filter)),shading flat
% Apply the filter to get the results
lat_resultado = lat_h12v09(combined_filter);
lon_resultado = lon_h12v09(combined_filter);
% plots para visualizar se os resultados estao ok
% figure,plot(lon_resultado)
% figure,plot(lat_resultado)
% figure,plot(lat_resultado,lon_resultado)
%%
% Plotting
% Define the corners of the square boundary
x = [lon_bounds(1), lon_bounds(2), lon_bounds(2), lon_bounds(1), lon_bounds(1)];
y = [lat_bounds(1), lat_bounds(1), lat_bounds(2), lat_bounds(2), lat_bounds(1)];
% Create the plot of the square with a transparent background
figure;
plot(x, y, 'b','LineWidth',4); % Blue lines for the square boundary
hold on; % Hold on to plot additional data on the same figure
scatter(lon_resultado, lat_resultado, 'r.'); % Plot the filtered points in red
% Additional plot formatting
xlabel('Longitude');
ylabel('Latitude');
title('Filtered Lat/Lon Points within Specified Bounds');
legend('Boundary', 'Filtered Points');
grid on; % Optional: Add a grid to the plot for better readability
% Check the results
disp('Number of points within the boundary:');
disp(sum(combined_filter)); % Display the count of points within the boundary
%%
% LatIndex=find(all(combined_filter,2));
% LonIndex=find(all(lon_filter,2));
[LonIndex, ~] = find(any(lon_filter == 1, 2));
[~, LatIndex] = find(any(lat_filter == 1, 1));
%%
erros_file_list = [];
for i = 1:length(file_list)
file_path = fullfile(dir_path, file_list(i).name);
disp(['Lendo arquivo: ', file_list(i).name]); % Nunca esquecer de adicionar esse comando nos scripts
try
load(file_path, variable_name); % Carrega a variável do arquivo .mat
% Filtrar os dados com base nas latitudes e longitudes desejadas
aod_data1 = eval(variable_name(:,:,1)); % Extrai a variável carregada
aod_data1 = aod_data1(LonIndex, LatIndex, 1);
% Adicionar os dados filtrados à matriz 'data'
data_aod = cat(3, data_aod, aod_data1);
catch ME
% Se houver um erro ao ler o arquivo .mat, exiba a mensagem de erro e continue para o próximo arquivo
disp(['Erro ao processar o arquivo ' fn(i).name]);
disp(ME.message);
erros_file_list = [erros_file_list; i]; % Adiciona o número do arquivo com erro à matriz erros_file_list
continue
end
end
6 Comments
Star Strider
on 3 Jan 2024
When I did whos after load, there is only ‘data’ (and a couple other variables generated by the whos call).
Loading into a structure reveals —
S = load('data_mat_h12v09_2010_195.mat')
That’s everything in the file.
Accepted Answer
Sulaymon Eshkabilov
on 3 Jan 2024
In this case, the combined filter data should be used:
[LonIndex, ~] = find(any(combined_filter, 2));
[~, LatIndex] = find(any(combined_filter, 1));
Instead of:
[LonIndex, ~] = find(any(lon_filter == 1, 2));
[~, LatIndex] = find(any(lat_filter == 1, 1));
3 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!