How to save 7 different large matrix (for Landsat 7 bands) in one variable?

1 vue (au cours des 30 derniers jours)
Meghraj Kc
Meghraj Kc le 3 Oct 2022
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 commentaires
dpb
dpb le 4 Oct 2022
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 le 4 Oct 2022
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.

Connectez-vous pour commenter.

Réponses (1)

Bjorn Gustavsson
Bjorn Gustavsson le 4 Oct 2022
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 commentaires
Meghraj Kc
Meghraj Kc le 4 Oct 2022
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 le 5 Oct 2022
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

Connectez-vous pour commenter.

Communautés

Plus de réponses dans  Power Electronics Control

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by