I have two ideas.
1. Use for-loop
dataDir = '*.hdf';
listing = dir(dataDir);
for ii=1:size(listing, 1)
fname = listing(ii).name;
Red{ii} = hdfread(fname, 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{ii} = hdfread(fname, 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
end
2. Use datastore
dataDir = '*.hdf';
readFcn = @(filename) readModis(filename);
fds = fileDatastore(dataDir, 'ReadFcn', readFcn);
ii = 1;
while hasdata(fds)
s = read(fds);
Red{ii} = s.red;
NIR{ii} = s.nir;
ii = ii+1;
end
function data = readModis(fpath)
data.red = hdfread(fpath, 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
data.nir = hdfread(fpath, 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
end
Best Answer