Using find to check the contents of files within a struct
Show older comments
Hello, I am working on a project in which I have a 5122x1 struct containing files with latitude and longitude data. I access the files in the struct with a for loop. ie)
latlon_data = 'C:\Users\bbush\Desktop\Matlab Code and Plots\Microwave Files\20190407\GATMO'; %Need to change dated folder to the correct day
latlonfn = dir(fullfile(latlon_data,'*_*_*_*_*_*_*_*_*.h5')); % return the list of all files
for i = 1:length(latlonfn)
%lat/lon and bt variables with all of the unmatched files
lat = double(h5read(latlonfn(i).name, '/All_Data/ATMS-SDR-GEO_All/Latitude'));
lon = double(h5read(latlonfn(i).name, '/All_Data/ATMS-SDR-GEO_All/Longitude'));
emptyIndex = find(latlonfn(i).name((lat >= 20 & lat <= 60) && (lon >= -128 & lon <= -65)));
end
These files contain satellite swaths that cover the entire planet but I need to be able to figure out which ones are only over the U.S. I am having trouble implementing the find function to to search through the struct and look at the lat and lon points. I need to return the index of files within the struct that have lat and lon points inside the given ranges.
I have tried using double && inside the comparisons as well but that didnt work, I'm just not sure how to set up the find command
3 Comments
Brandon Bush
on 21 Aug 2019
Edited: Brandon Bush
on 21 Aug 2019
Guillaume
on 21 Aug 2019
Note: strictly speaking your structure does not contain files. It contains, among other things, filenames. The files are stored on the hard drive.
As Jon has pointed out your find syntax is completely wrong, and I'm not sure what indices you're actually wanting. The indices of the elements of the structure (i.e filenames) for which the matrices lat and lon have some (or is all?) coordinates within your band? Or the indices of lat and lon which are within the band for each element of the structure? Or both?
Also, isn't there a function in the mapping toolbox for finding if a set of coordinates is within a country. That would be better than assuming the US is rectangular (or whatever the projection of a rectangle on sphere is called).
Brandon Bush
on 21 Aug 2019
Accepted Answer
More Answers (1)
Brandon Bush
on 21 Aug 2019
2 Comments
Glad you were able to anwer your question.
Note that you have some redundant lines of code. No need for the if statement. You could streamline that to
in(i) = any(inpolygon(lon,lat,xv,yv)) %determines if the lat and lon values are inside the polygon
Also since your polygon is just a rectangle you don't need all of the machinery of inpolygon. I would guess that it is simpler and more efficient to check it as shown in my final comment to you, with just simple <= range checks
Brandon Bush
on 21 Aug 2019
Categories
Find more on Matrix Indexing 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!