【问题标题】:Matlab iterating through files in a folder, and check if the file name has specific charactersMatlab遍历文件夹中的文件,并检查文件名是否具有特定字符
【发布时间】:2015-10-07 06:21:57
【问题描述】:

我有一些相同维度的气候数据文件 (netcdf)。

例如: 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

谁能帮我完成这段代码?

谢谢

【问题讨论】:

标签: matlab loops filenames netcdf


【解决方案1】:

据我了解,ncFile 包含文件列表,您想根据文件名区分某些文件吗?

如果你想这样做,你可以这样做:

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

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

【讨论】:

  • 谢谢,苏珊特。这正是我想要的。我会尝试完成代码,看看它是否运行良好。
猜你喜欢
  • 1970-01-01
  • 2018-10-31
  • 2019-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多