Open bands from several MODIS scenes with a loop

1 次查看(过去 30 天)
Hello, I would like to know if there is any alternative way to open all these bands in a single loop ?
Red{1} = hdfread('MOD09GA.A2004020.h17v05.006.2015085050751.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{1} = hdfread('MOD09GA.A2004020.h17v05.006.2015085050751.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{2} = hdfread('MOD09GA.A2004047.h17v05.006.2015086041209.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{2} = hdfread('MOD09GA.A2004047.h17v05.006.2015086041209.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{3} = hdfread('MOD09GA.A2004080.h17v05.006.2015087072318.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{3} = hdfread('MOD09GA.A2004080.h17v05.006.2015087072318.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{4} = hdfread('MOD09GA.A2004114.h17v05.006.2015088010956.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{4} = hdfread('MOD09GA.A2004114.h17v05.006.2015088010956.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{5} = hdfread('MOD09GA.A2004140.h17v05.006.2015089030618.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{5} = hdfread('MOD09GA.A2004140.h17v05.006.2015089030618.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{6} = hdfread('MOD09GA.A2004177.h17v05.006.2015090215040.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{6} = hdfread('MOD09GA.A2004177.h17v05.006.2015090215040.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{7} = hdfread('MOD09GA.A2004209.h17v05.006.2015092061947.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{7} = hdfread('MOD09GA.A2004209.h17v05.006.2015092061947.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{8} = hdfread('MOD09GA.A2004243.h17v05.006.2015093025827.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{8} = hdfread('MOD09GA.A2004243.h17v05.006.2015093025827.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{9} = hdfread('MOD09GA.A2004272.h17v05.006.2015094101457.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{9} = hdfread('MOD09GA.A2004272.h17v05.006.2015094101457.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{10} = hdfread('MOD09GA.A2004290.h17v05.006.2015095025852.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{10} = hdfread('MOD09GA.A2004290.h17v05.006.2015095025852.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{11} = hdfread('MOD09GA.A2004321.h17v05.006.2015096045825.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{11} = hdfread('MOD09GA.A2004321.h17v05.006.2015096045825.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');
Red{12} = hdfread('MOD09GA.A2004353.h17v05.006.2015097022651.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b01_1');
NIR{12} = hdfread('MOD09GA.A2004353.h17v05.006.2015097022651.hdf', 'MODIS_Grid_500m_2D', 'Fields', 'sur_refl_b02_1');

采纳的回答

Kojiro Saito
Kojiro Saito 2018-11-1
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

更多回答(0 个)

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by