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
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?
Meghraj Kc
Meghraj Kc 2022-10-4
Sorry to misinterpret, you are correct i.e., bands are 1:7
I mean to say that the size of "A1" is 7321*7351 so, at the end when multiplying and adding with my scaling factor. I don't know how to save it for 7 bands.

请先登录,再进行评论。

回答(1 个)

Bjorn Gustavsson
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 个评论
Meghraj Kc
Meghraj Kc 2022-10-4
Now I am able to save per band. Also, I want to save per date also since, I have 161 total dates. It is not possible to run for individual date. How can I save per band?
clear all
if ispc
filepath1 = ('Z:\ImageDrive\OLI.TIRS\L8\P039\R037');
elseif isunix
filepath1 = ('/home/megh.kc/zdrive/ImageDrive/OLI.TIRS/L8/P039/R037');
end
date1 = dir(filepath1);
date1 ([1 2])= [];
for dates1 =1:161%size(date1,1)
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);
Bjorn Gustavsson
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

请先登录,再进行评论。

社区

更多回答在  Power Electronics Control

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by