I know how to extract data for a range of latitude and longitude from a NetCDF file. Doing this is leading me to have a rectangular on my study region, which some part of it has been offside. Some pixels are outside of the country. See the below figure:
the red line is country borders, and you'll see some pixel are outside.
I tried do overcome this problem and found a shapefile of the country that contains bounding box and X, Y data of all across the boundary.
Now I want to ask you how to use this X and Y (in the 1x1 struct with six fields) to cut my study region from the NetCDF file precisely. or set every pixel outside of the border to NaN.
I'll attach shapefile and my code to read the NetCDF file.
lon = ncread(filename,'longitude');
lat = ncread(filename,'latitude');
precipitation = ncread(filename,'precip');
t = ncread(filename,'time');
precipitation size is : lon x lat x t
So, in summary, I want to know how to set NaN values for any pixel outside of country.