필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Meghraj Kc
Meghraj Kc 2022년 10월 3일
댓글: Bjorn Gustavsson 2022년 10월 5일
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 Community

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by