Read multiple netcdf files with variable names

3 次查看(过去 30 天)
I got around 1000 netcdf-files that needs to be run in the same matlab-script and have problems getting them loaded. The files are named in a logical format, like this:
model_variable_period_area
where model=model1,model2,model3..
variable=variable1,variable2,variable3...
period=period1...
area=area1..
So every model, has multiple variables, which again has multiple periods, and so on, for example could a filename be:
model3_variable2_period3_area4
But there will be exceptions, cases where the file doesn't exist, for example if period3 only exist together with model1, so that the filename
model2_variable2_period3_area4
wouldn't exist, and Matlab wouldn't be able to find it.
So now to my question - I have tried several methods, but I don't know how to do this. I'm using ncread to load the files, and I've tried something like this:
model = genvarname({'model1', 'model2', 'model3', 'model4'});
variable = genvarname({'variable1','variable2','variable3',variable4'});
area = genvarname({'area1','area2','area3','area4'});
if model = 'model1';
period = genvarname({'period3'});
else period = genvarname({'period1','period2','period3','period4'});
end
Input_file = ncread(sprintf('%d_%d_%d_%d',model,variable,period,area),'varname');
Thanks in advance for your help!

回答(1 个)

Ashish Uthama
Ashish Uthama 2013-3-13
You could think of something along these lines:
(air code, as in I havent tested it)
modelNames = {'m1, m2};
varNames = {'a','b'};
for mInd = 1:numel(modelNames)
fileName = modelnames{mInd};
for vInd = 1:numel(varNames)
if(exist(fileName,'f')) % please see doc, I think there is a syntax option to check for the existence of a file using its name
try
varName = varNames{vInd};
data = ncread(fileName,varName);
catch ALL % better to see one failure when the variable does not exist, use lasterr and check for the specific exception issued.
disp(['Could not read ' filname' for ' varName]);
end
end
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by