how to create for loop in matlab?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi All,
I have thousands of images each date has 7 bands. I need to create for-loop to calculate
MNDWI=(b4-b6)/(b4+b6)
here is example of the images
'MOD09A1.A2000057.h21v05.006.2015136062042.hdf_sur_refl_b01_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000057.h21v05.006.2015136062042.hdf_sur_refl_b02_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000057.h21v05.006.2015136062042.hdf_sur_refl_b03_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000057.h21v05.006.2015136062042.hdf_sur_refl_b04_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000057.h21v05.006.2015136062042.hdf_sur_refl_b05_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000057.h21v05.006.2015136062042.hdf_sur_refl_b06_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000057.h21v05.006.2015136062042.hdf_sur_refl_b07_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000065.h21v05.006.2015136062016.hdf_sur_refl_b01_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000065.h21v05.006.2015136062016.hdf_sur_refl_b02_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000065.h21v05.006.2015136062016.hdf_sur_refl_b03_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000065.h21v05.006.2015136062016.hdf_sur_refl_b04_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000065.h21v05.006.2015136062016.hdf_sur_refl_b05_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000065.h21v05.006.2015136062016.hdf_sur_refl_b06_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000065.h21v05.006.2015136062016.hdf_sur_refl_b07_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000073.h21v05.006.2015136060905.hdf_sur_refl_b01_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000073.h21v05.006.2015136060905.hdf_sur_refl_b02_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000073.h21v05.006.2015136060905.hdf_sur_refl_b03_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000073.h21v05.006.2015136060905.hdf_sur_refl_b04_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000073.h21v05.006.2015136060905.hdf_sur_refl_b05_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000073.h21v05.006.2015136060905.hdf_sur_refl_b06_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000073.h21v05.006.2015136060905.hdf_sur_refl_b07_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000081.h21v05.006.2015136075413.hdf_sur_refl_b01_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000081.h21v05.006.2015136075413.hdf_sur_refl_b02_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000081.h21v05.006.2015136075413.hdf_sur_refl_b03_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000081.h21v05.006.2015136075413.hdf_sur_refl_b04_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000081.h21v05.006.2015136075413.hdf_sur_refl_b05_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000081.h21v05.006.2015136075413.hdf_sur_refl_b06_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000081.h21v05.006.2015136075413.hdf_sur_refl_b07_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000089.h21v05.006.2015136062547.hdf_sur_refl_b01_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000089.h21v05.006.2015136062547.hdf_sur_refl_b02_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000089.h21v05.006.2015136062547.hdf_sur_refl_b03_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000089.h21v05.006.2015136062547.hdf_sur_refl_b04_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000089.h21v05.006.2015136062547.hdf_sur_refl_b05_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000089.h21v05.006.2015136062547.hdf_sur_refl_b06_clip.tif_mosaico_ream.tif'
'MOD09A1.A2000089.h21v05.006.2015136062547.hdf_sur_refl_b07_clip.tif_mosaico_ream.tif'
Thank you in Advance
Reyadh
댓글 수: 3
per isakson
2017년 4월 4일
편집: per isakson
2017년 4월 4일
I still don't understand how to find the two numbers to put into this expression
(b4-b6)/(b4+b6)
답변 (1개)
hamed amini
2017년 4월 4일
편집: per isakson
2017년 4월 4일
look into sprintf function
>> variable_numbers1 = [1 2 3 ... 1000];
>> variable_numbers2 = [2001 2002 2003 ... 2001000];
>> for i=1:1000
>> eval(sprintf('image_bank.image%d = imread(''fixed_string%d_fixed_string%d.tiff'');',i,variable_numbers1(i),variable_numbers2(i)));
>> end
댓글 수: 2
Jan
2017년 4월 4일
편집: Jan
2017년 4월 5일
Do not create variables dynamically using eval. See https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Prefer:
image_bank.image = cell(1, 1000);
for i = 1:1000
filename = sprintf('fixed_string%d_fixed_string%d.tiff', ...
i, variable_numbers1(i));
image_bank.image{i} = imread(filename);
end
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!