FOR loop through multiple datasets

23 views (last 30 days)
Elizabeth Yeap
Elizabeth Yeap on 9 Dec 2019
Edited: Elizabeth Yeap on 10 Dec 2019
Hello,
I want to create a for loop that will sum specific columns of a single row of a matrix in a dataset. I then want to repeat that action for other datasets. The matrix is 37 rows x 100 columns. All datasets are from one mat. file.
% For Nubian Aquifer - 1900-1999
sum_ET = sum(aq_gwdctr_ET(1,:))/100;
sum_P = sum(aq_gwdctr_P(1,:))/100;
sum_R = sum(aq_gwd_qover(1,:))/100;
sum_dSdt = sum(aq_gwdctr_dSdt(1,:))/100;
sum_C = sum(aq_gwdctr_C(1,:))/100;
sum_GWpumping = sum(aq_irri4d(1,:))/100;
% For Nubian Aquifer - 1950-1999
sum_ET1 = sum(aq_gwdctr_ET(1,51:end))/50;
sum_P1 = sum(aq_gwdctr_P(1,51:end))/50;
sum_R1 = sum(aq_gwd_qover(1,51:end))/50;
sum_dSdt1 = sum(aq_gwdctr_dSdt(1,51:end))/50;
sum_C1 = sum(aq_gwdctr_C(1,51:end))/50;
sum_GWpumping1 = sum(aq_irri4d(1,51:end))/50;
% For Nubian Aquifer - 1900-1949
sum_ET2 = sum(aq_gwdctr_ET(1,1:50))/50;
sum_P2 = sum(aq_gwdctr_P(1,1:50))/50;
sum_R2 = sum(aq_gwd_qover(1,1:50))/50;
sum_dSdt2 = sum(aq_gwdctr_dSdt(1,1:50))/50;
sum_C2 = sum(aq_gwdctr_C(1,1:50))/50;
sum_GWpumping2 = sum(aq_irri4d(1,1:50))/50;
Currently, the code that I have performs all these individually. What I'm hoping to achieve is to simplify the above code. The new code should be able to;
A) For 1900-1999, sum all the columns for every row for each matrix, dataset. For 1950-1999, sum columns 51 until end for every row for each matrix, dataset. For 1900-1949, sum columns 1 until 50 for every row for each matrix, dataset.
B) This action should be repeated for each dataset (aq_gwdctr_ET, aq_gwdctr_P, etc.).
C) It would be a bonus if I could tabulate all these values.
% For year 1900-1999
Aquifer = [1; 2; 3]; % This is the rows in the matrix
ET = [sum_ET]; % Sum ET for each row in the matrix, repeat for each dataset
T = table(Aquifer, ET);
Thank you for your help.
  3 Comments
Elizabeth Yeap
Elizabeth Yeap on 10 Dec 2019
@Walter, the mat files in the link that you posted are sequentially numbered which is not the case for my mat files. How would I change the example to fit my case?
Walter Roberson
Walter Roberson on 10 Dec 2019
projectdir = 'name of directory files are in';
fileext = 'mat';
dinfo = dir(fullfile( projectdir, ['*.' fileext] ));
filenames = fullfile(projectdir, {dinfo.name});
numfiles = length(filenames);
T = cell(1, numfiles);
for K = 1 : numfiles
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
at this point analyze basename to figure out what year the file is for
at this point do whatever to load thisfile data
if file_year <= 1949
columns = 1:50;
else
columns = 51:size(aq_gwdctr_ET,2);
end
now analyze using columns as index
T{K} = table(Aquifer, ET);
end

Sign in to comment.

Answers (1)

Hiro Yoshino
Hiro Yoshino on 9 Dec 2019

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!