How to save 7 different large matrix (for Landsat 7 bands) in one variable?
1 次查看(过去 30 天)
显示 更早的评论
In this MATLAB code i want to save the surface reflectance for all the bands i.e., 7 bands in surface_Reflectance1 variable. The matrix size of one band is 7321*7351. So can you please suggest me how to save all the bands in one variable name.?
for dates1 =1:121
for bands = 1:7
if exist(fullfile(filepath1,date1(dates1).name,'L2C2'), 'dir')
date_dir1 = dir(fullfile(filepath1,date1(dates1).name,'L2C2'));
ImB1file1 = dir(fullfile(filepath1,date1(dates1).name,'L2C2','*B1.TIF'));
Imfiles1_N = dir(fullfile(filepath1,date1(dates1).name,'L2C2',[ImB1file1.name(1:end-5),num2str(bands),'.TIF']));
[A1, R] = geotiffread(fullfile(Imfiles1_N.folder,Imfiles1_N.name));
Mtl2 = dir(fullfile(filepath1,date1(dates1).name,'L2C2','*MTL.txt'));
[MTL_list1,value] = MTL_parser_L8(fullfile(Mtl2.folder,Mtl2.name));
RMUL1 = 0.0000275;
RADD1 = -0.2;
Surface_Reflectance1(:,bands) = (RMUL1)*(single(A1))+(RADD1);
end
end
end
2 个评论
dpb
2022-10-4
Makes no sense...you wrote "The matrix size of one band is 7321*7351." but in
Surface_Reflectance1(:,bands) = (RMUL1)*(single(A1))+(RADD1);
variable bands is 1, 2, ..., 7 subsequently so Surface_Reflectance1(:,bands) is and can only be a Nx7 array. Where are the other 7351-7 columns supposed to be coming from?
回答(1 个)
Bjorn Gustavsson
2022-10-4
Break down your loops into something simpler first. Skip the outermost one and select one date that you know you have data from.
Also change your coding style, at least for the loop-variables. It is a mess to read date1(dates1) - that will be a source for unnecessary headakes. Change dates1 to i_date, or idx_dates just to make it clear what is the dates-array and what is the indices into that array. This will make a big difference in the long run.
% for i_dates =1:121
i_dates = 12; % if you have dat from that date
for bands = 1:7
if exist(fullfile(filepath1,date1(i_dates).name,'L2C2'), 'dir')
date_dir1 = dir(fullfile(filepath1,date1(i_dates).name,'L2C2'));
ImB1file1 = dir(fullfile(filepath1,date1(i_dates).name,'L2C2','*B1.TIF'));
Imfiles1_N = dir(fullfile(filepath1,date1(i_dates).name,'L2C2',...
[ImB1file1.name(1:end-5),num2str(bands),'.TIF']));
[A1, R] = geotiffread(fullfile(Imfiles1_N.folder,Imfiles1_N.name));
Mtl2 = dir(fullfile(filepath1,date1(i_dates).name,'L2C2','*MTL.txt'));
[MTL_list1,value] = MTL_parser_L8(fullfile(Mtl2.folder,Mtl2.name));
RMUL1 = 0.0000275;
RADD1 = -0.2;
% Something like this will save a 7321 x 7351 image A1 into Surface_reflectance
% for each band.
Surface_Reflectance1(:,:,bands) = (RMUL1)*(single(A1))+(RADD1);
end
end
HTH
3 个评论
Bjorn Gustavsson
2022-10-5
My suggestion is that you save data from separate dates into different directories. Something like this:
for iDate = 1:numel(Dates)
currDirname = datestr(Dates(iDate,:),'yyyymmdd'); % or whatever format suits you
mkdir(currDirname)
% reading data
% processing data
savefile = sprintf('My-process.mat')
savename = fullfile(currDirname,savefile)
save(savename,'Surface_Reflectance1') % add other variables, or whatever
end
HTH
社区
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!