MATLAB: Is it possible to write a loop for doing this

for loopMATLAB

Hi, I don't know how to write a for loop to do this using some for loops in order to reduce lines:
nCELL = numel(MERRA2_selected);
newCELL = cell(2,nCELL );
for i=1:nCELL
date_month = month(MERRA2_selected{i}.date);
jan_mask = date_month == 1;
feb_mask = date_month == 2;
mar_mask = date_month == 3;
apr_mask = date_month == 4;
may_mask = date_month == 5;
jun_mask = date_month == 6;
jul_mask = date_month == 7;
aug_mask = date_month == 8;
sep_mask = date_month == 9;
oct_mask = date_month == 10;
nov_mask = date_month == 11;
dec_mask = date_month == 12;
jan_precip = MERRA2_selected{i}.precip(jan_mask);
feb_precip = MERRA2_selected{i}.precip(feb_mask);
mar_precip = MERRA2_selected{i}.precip(mar_mask);
apr_precip = MERRA2_selected{i}.precip(apr_mask);
may_precip = MERRA2_selected{i}.precip(may_mask);
jun_precip = MERRA2_selected{i}.precip(jun_mask);
jul_precip = MERRA2_selected{i}.precip(jul_mask);
aug_precip = MERRA2_selected{i}.precip(aug_mask);
sep_precip = MERRA2_selected{i}.precip(sep_mask);
oct_precip = MERRA2_selected{i}.precip(oct_mask);
nov_precip = MERRA2_selected{i}.precip(nov_mask);
dec_precip = MERRA2_selected{i}.precip(dec_mask);
newCELL{1,i} = jan_precip;
newCELL{2,i} = feb_precip;
newCELL{3,i} = mar_precip;
newCELL{4,i} = apr_precip;
newCELL{5,i} = may_precip;
newCELL{6,i} = jun_precip;
newCELL{7,i} = jul_precip;
newCELL{8,i} = aug_precip;
newCELL{9,i} = sep_precip;
newCELL{10,i} = oct_precip;
newCELL{11,i} = nov_precip;
newCELL{12,i} = dec_precip;
end
MERRA2_monthly = newCELL;
MERRA2_monthly = cellfun(@array2table,MERRA2_monthly, 'UniformOutput', 0);
for o = 1:92
MERRA2_monthly{1, o}.Properties.VariableNames{1} = 'january';
MERRA2_monthly{2, o}.Properties.VariableNames{1} = 'february';
MERRA2_monthly{3, o}.Properties.VariableNames{1} = 'march';
MERRA2_monthly{4, o}.Properties.VariableNames{1} = 'april';
MERRA2_monthly{5, o}.Properties.VariableNames{1} = 'may';
MERRA2_monthly{6, o}.Properties.VariableNames{1} = 'june';
MERRA2_monthly{7, o}.Properties.VariableNames{1} = 'july';
MERRA2_monthly{8, o}.Properties.VariableNames{1} = 'august';
MERRA2_monthly{9, o}.Properties.VariableNames{1} = 'september';
MERRA2_monthly{10, o}.Properties.VariableNames{1} = 'october';
MERRA2_monthly{11, o}.Properties.VariableNames{1} = 'november';
MERRA2_monthly{12, o}.Properties.VariableNames{1} = 'december';
end
MERRA2_Monthly_Jan = MERRA2_monthly(1,:);
MERRA2_Monthly_Feb = MERRA2_monthly(2,:);
MERRA2_Monthly_Mar = MERRA2_monthly(3,:);
MERRA2_Monthly_Apr = MERRA2_monthly(4,:);
MERRA2_Monthly_May = MERRA2_monthly(5,:);
MERRA2_Monthly_Jun = MERRA2_monthly(6,:);
MERRA2_Monthly_Jul = MERRA2_monthly(7,:);
MERRA2_Monthly_Aug = MERRA2_monthly(8,:);
MERRA2_Monthly_Sep = MERRA2_monthly(9,:);
MERRA2_Monthly_Oct = MERRA2_monthly(10,:);
MERRA2_Monthly_Nov = MERRA2_monthly(11,:);
MERRA2_Monthly_Dec = MERRA2_monthly(12,:);

Best Answer

Behzad - look for what is common. Your code has twelve months, so you should be able to replace that with a loop. Perhaps something like
nCELL = numel(MERRA2_selected);
newCELL = cell(2,nCELL );
for i=1:nCELL
date_month = month(MERRA2_selected{i}.date);
for k = 1:12
month_mask = date_month == k
month_precip = MERRA2_selected{i}.precip(month_mask);
newCELL{k,i} = month_precip;
end
end