How to extract data from nc file by using shape file in MATLAB
Show older comments
How to extract data from nc file by using shape file in matlab. I am sending a sample .nc file of resolution 1*1 degree and shapefile of India.
3 Comments
Tanmoyee Bhattacharya
on 29 Apr 2016
Walter Roberson
on 30 Apr 2016
Nothing was attached
Tanmoyee Bhattacharya
on 4 May 2016
Answers (3)
clc; clear all ;
file = 'cpc_rfe_v2.0_sa_dly.bin.20020101.nc' ;
lon = ncread(file,'lon') ;
lat = ncread(file,'lat') ;
RF = ncread(file,'RF') ;
%
path = [pwd filesep 'India State wise with telangana' filesep 'india27-11.shp'] ;
S = shaperead(path) ;
n = length(S) ;
pcolor(lon,lat,RF') ; shading interp ;
hold on
for i = 1%:n
x = S(i).X ; y = S(i).Y ;
rfi = interp2(lon,lat,RF',x,y) ;
end
Your next question is: 'I want data along the polygon, not along the boundary'. It is a project work and you expect everything online?
1 Comment
Muhammad Usman Saleem
on 25 Feb 2017
@KSSV dear, will you tell me how can i get data along the complete polygon ?
Jitesh Dadich
on 9 Apr 2019
1 vote
this really helped me....
Dear @KSSV, what if the time is also given in the nc file i.e. nc data are in 3D...then how will we extract the data from .nc file according to given shapefile....
3 Comments
Jitesh Dadich
on 9 Apr 2019
and can we save the output in .nc format file..?
KSSV
on 9 Apr 2019
Read about inpolygon.
ANKUR KUMAR
on 9 May 2019
@Jitesh Refer to my answer.
ANKUR KUMAR
on 9 May 2019
Edited: ANKUR KUMAR
on 9 May 2019
0 votes
Example you can refer to for your convenience
W=shaperead('us_states.shp');
[data_cropped]=maskregion(lon,lat,data,W);
contourf(lon,lat,nanmean(data_cropped,3)','linecolor','none')

5 Comments
Saumya Singh
on 17 Jul 2021
Hi Ankur,
tried maskregion function, but the output produces matrix of same dimension with all values as NaN. Its not working.
ANKUR KUMAR
on 18 Jul 2021
Could you please attach the mat file, along with the shape file?
Saumya Singh
on 19 Jul 2021
Edited: Saumya Singh
on 19 Jul 2021
Saumya Singh
on 20 Jul 2021
here is the shapefile
Suraj
on 13 Jun 2023
hi saumya did u find the solution while using the maskregion!
Categories
Find more on Descriptive Statistics and Visualization 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!