Reading multiple nc files in different subfolders within a directory
Show older comments
Hi,
My data is stored in a folder withch contains subfolders cyc001, cyc002, cyc003...cyc258 each with 15 .nc files. I have written this code to explore the main data data directory and each cyc* subdirectories (total 258),read data from 15 nc files, retrieve certain fields in each .nc filesand store/save as output (passes). My code works well for one cyc001 folder but it failes to read the 2nd and subsequen folders.
currentdir= '/Users/Documents/test1';
cd(currentdir);
% For each cyc subdirectory
list_cycle= textscan(ls(),'%s');
cyclelist=list_cycle{1,1}(:,:)
j=1;
for c=1:length(cyclelist)
cd(cyclelist{c,1});%change into current cyc
list_dir= textscan(ls('-d','*.nc'),'%s');
dirlist=list_dir{1,1}(:,:)
%read nc files
myFolder=pwd;% folder of each cy
filePattern = fullfile(myFolder, '*.nc');
theFiles = dir(filePattern);
findFileInFolder(pwd,{'.nc'})
% Loop for each nc-file
for k = 1:length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
ncid=netcdf.open(fullFileName, 'NC_NOWRITE');
varname= netcdf.inqVar(ncid,4);
varid = netcdf.inqVarID(ncid,varname);
netcdf.close(ncid);
end
ncfiles = dir('*.nc'); % get all nc files in the folder
nfiles = length(ncfiles) ; % total number of files
end
% For each pass (.nc file)
for d=1:length(dirlist)
cd(dirlist{d,1});
data=read_nc(fullFileName); % Read the selected nc files using read_nc func
fprintf('Reading track: %d/%d of pass %d/%d. To read tracks: %d\n',d, length(dirlist),c,length(cyclelist),i);
ind=find((data.glat.Value<lat1)&(data.glat.Value>lat2)&(data.glon.Value<lon1|data.glon.Value>lon2));%find the indices
passe(j).lat= data.glat.Value(ind); % copy the fields in the data structure
passe(j).lon= data.glon.Value(ind); % copy the fields in the data structure
j=j+1;
clear data;
cd('..')
end
cd('..')
cd(currentdir)
It gives me eorror that
Error using cd
Cannot CD to cycle002 (Name is nonexistent or not a directory).
Ithink porblem is where I am trying to read nc files in each cyc... subfolder. Anyone, please help to fix this loop? I am not an expert matlab user so maybe I cam forgetting something and doing something wrong here.
1 Comment
Do NOT use cd like that. Do NOT change directories just to access data files. It is faster and more robust to use absolute/relative filenames, which you can easily construct using fullfile.
Do NOT use ls in code to get a list of folder contents (it is intended for visual display, not for processing in code). Use dir instead.
Accepted Answer
More Answers (1)
KSSV
on 11 Dec 2018
ncfiles = dir('*nc') ;
N = length(ncfiles) ;
for i = 1:N
ncfile = ncfiles(i).name ;
% do what you want
end
Categories
Find more on Just for fun 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!