2015-10-06 25 views
0

我有一些相同维度的气候数据文件(netcdf)。Matlab遍历文件夹中的文件,并检查文件名是否具有特定字符

例如: agg_humidity_bc_historical_1980_2001.nc

agg_humidity_bc_future_2020_2040.nc

agg_wind_bc_historical_1980_2001.nc

agg_precipitation_bc_future_2020_2040.nc

.....

我有一个计划MATLAB从中提取特定的数据点每一个文件。我想迭代所有文件,检查文件名中的变量名,例如湿度,风,降水等,并根据变量提取数据。然后我想存储这些提取的值与NC文件同名到CSV文件,如:

agg_humidity_bc_future_2020_2040.csv

agg_wind_bc_historical_1980_2001.csv

agg_precipitation_bc_future_2020_2040.csv

这里是我现在有的代码。

mkdir test 
data=dir('*.nc'); 

    for i=1:length(data) 
    ncFile=data(i).name 

    ??? How to check which variable is in the ncFile? 

    %%Got the index of the location of 
    LonInd=22; 
    LatInd=10; 

    if variable=humidity 
    SH=ncread(ncFile,'humidity',[LonInd, LatInd, 1], [1 1 inf]); 
    SH=squeeze(SH); 
    fid = fopen(['test\' ncFile.csv],'w'); 
    fprintf(fid,%d,SH) 
    else if variable=wind 
    wind=ncread(ncFile,'wind',[LonInd, LatInd, 1], [1 1 inf]); 
    wind=squeeze(wind); 
    fid = fopen(['test\' ncFile.csv],'w'); 
    fprintf(fid,%d,wind) 
    fid = fclose(fid); 
    fid = fclose(fid); 
    else if variable=wind 
    precipitation=ncread(ncFile,'precipitation',[LonInd, LatInd, 1], [1 1 inf]); 
    precipitation=squeeze(precipitation); 
    fid = fopen(['test\' ncFile.csv],'w'); 
    fprintf(fid,%d,precipitation) 
    fid = fclose(fid); 
end 

任何人都可以帮我完成这段代码吗?

感谢

+0

http://www.mathworks.com/help/matlab/ref/regexp.html – user3528438

回答

2

随着我了解,NCFILE包含文件的列表中,您要根据文件名来区分特定的文件?

如果这是你想要做他们什么,你可以这样做:

ncFile = data(1).name 
result = findStr(ncFile, 'desired file name') 

然后或检查结果为空不是(你可以使用isempty)。如果结果为空,则ncFile不是您要查找的内容。

+0

谢谢,Sushant。这正是我想要的。我会尽力完成代码,看看它是否正常。 – James

相关问题