Regarding reading diffrent files

I have some .nc files for 13 years (13 files) e.g GTE_bb_CH_1997.nc,GTE_bb_CH_1998.nc,...., GTE_bb_CH_2009.nc I want to read and plot all files same time. So how I can write loop so I can read separate file for every year ?

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日

0 投票

Files=dir('*.nc');
for k=1:length(Files)
FileName=Files(k).name;
%load file
end

7 件のコメント

Uday
Uday 2011 年 9 月 19 日
how to load file here?
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日
That depends. What is .nc file? There is no built-in function to load .nc file directly. If it's a text file, you have to understand its format and then load it use uiimport(),importdata(), textscan() or fopen(), fread().
Uday
Uday 2011 年 9 月 19 日
netcdf is special data file, generally its stores huge amount of data.
I have used following codes to read files , it works for single file , but I would like to run all once
path=('c:/GEFD/');
dir_list=dir('*.nc');
for i=1:length(dir_list)
FileName=dir_list(i).name;
ncid=netcdf.open(FileName,'NOWRITE');
latid=netcdf.inqVarID(ncid,'lat');
latitude=double(netcdf.getVar(ncid,latid));
lonid=netcdf.inqVarID(ncid,'lon');
long=double(netcdf.getVar(ncid,lonid));
longitude=rem((long+180),360)-180;% conversion from 0-360 to -180 to 180
emiss_bbid=netcdf.inqVarID(ncid,'emiss_bb');
bb=double(netcdf.getVar(ncid,emiss_bbid));
Uday
Uday 2011 年 9 月 19 日
but these codes are not reading all files
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日
Okay, Thank you! I learned netcdf() today! It is reading all files. But you are over-writing the data every time, right? You need to add an "end" line to complete the for-loop. To store data for every file, you need to declare an array before the for-loop. Something like latitude=zeros(length(dir_list),1). Then, inside the for-loop, use latitude(i)=double(netcdf.getVar(ncid,latid));
Uday
Uday 2011 年 9 月 19 日
its not over writing
dir_list=dir('*.nc');
for i=1:length(dir_list)
FileName=dir_list(i).name;
ncid=netcdf.open(FileName,'NOWRITE');
the above para does not read the data files those I have in that folder ( 13 different year files).
Sorry I just forgot to wrote end in the previous codes.
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日
Try this:
dir_list=dir('*.nc');
for i=1:length(dir_list)
FileName=dir_list(i).name
end
Do you see 13 file names appear in the Command Window?

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2011 年 9 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by