hello, I need some suggestion
I know my code isn't perfect, but this is enough for me. just wondering if my code could be more compact or simpler. the point is the same only different in the grouping per 10 days. I only want to run my code once. here is my code.
for n=1:12
% Load data
ncfile = 'Temperature.nc';
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
% to read a variable 'var' exisiting in nc file
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
tempI = ncread(ncfile,'t2m');
% grup date
startDate = datetime(2021,1,1,0,0,0);
endDate = datetime(2021,12,31,23,0,0);
dates = startDate:hours(1):endDate;
%% pick each 10 days data
Onemonth = dates.Month==n ;
t = dates(Onemonth) ;
Ddays = t.Day<=10 ; %date 1-10
% Ddays = t.Day >= 11 & t.Day <=20 ; % date 11-20
% Ddays = t.Day >= 21 & t.Day <=31 ; % date 21-31
%%
% Extract data per month
t10days = tempI(:,:,Ddays);
% rotate data
tempII = flipud(rot90(t10days));
% Average data
temp = mean((tempII-273.15),3);
% Plot
figure
imagescn(lon,lat,temp);
colormap jet
axis image
end
for n=1:12
% Load data
ncfile = 'Temperature2m.nc';
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
% to read a variable 'var' exisiting in nc file
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
tempI = ncread(ncfile,'t2m');
%% grup date
startDate = datetime(2021,1,1,0,0,0);
endDate = datetime(2021,12,31,23,0,0);
dates = startDate:hours(1):endDate;
%% pick each 10 days data
Onemonth = dates.Month==n ;
t = dates(Onemonth) ;
% Ddays = t.Day<=10 ; %date 1-10
Ddays = t.Day >= 11 & t.Day <=20 ; % date 11-20
% Ddays = t.Day >= 21 & t.Day <=31 ; % date 21-31
%%
% Extract data per month
t10days = tempI(:,:,Ddays);
% rotate data
tempII = flipud(rot90(t10days));
% Average data
temp = mean((tempII-273.15),3);
%% Plot
figure
imagescn(lon,lat,temp);
colormap jet
axis image
end
for n=1:12
%% Load data
ncfile = 'Temperature2m.nc';
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
% to read a variable 'var' exisiting in nc file
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
tempI = ncread(ncfile,'t2m');
%% grup date
startDate = datetime(2021,1,1,0,0,0);
endDate = datetime(2021,12,31,23,0,0);
dates = startDate:hours(1):endDate;
%% pick each 10 days data
Onemonth = dates.Month==n ;
t = dates(Onemonth) ;
% Ddays = t.Day<=10 ; %date 1-10
% Ddays = t.Day >= 11 & t.Day <=20 ; % date 11-20
Ddays = t.Day >= 21 & t.Day <=31 ; % date 21-31
%%
% Extract data per month
t10days = tempI(:,:,Ddays);
% rotate data
tempII = flipud(rot90(t10days));
% Average data
temp = mean((tempII-273.15),3);
%% Plot
figure
imagescn(lon,lat,temp);
colormap jet
axis image
end

3 Comments

The 3 loops are identical except for the data file, even then 2nd and 3rd loop loads the same file.
What is your changing variable in all the codes? The file?
You can make your code a function file and call accordingly.
Den
Den on 28 Jun 2022
yes, all use the same file. therefore I want to make it more compact.
the only difference is the date. first code 1-10, second code 11-20, and 21-31.
I'm new to matlab. just in my understanding, function is used for repetitive for different files. I only use this code once. I just want to know if there is a way to simplify my code
See Jan's answer.

Sign in to comment.

 Accepted Answer

Jan
Jan on 28 Jun 2022
Move all repeated work out of the loops. In your case:
% Load data
ncfile = 'Temperature.nc';
months = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
% to read a variable 'var' exisiting in nc file
lon = ncread(ncfile,'longitude');
lat = ncread(ncfile,'latitude');
tempI = ncread(ncfile,'t2m');
% grup date
startDate = datetime(2021,1,1,0,0,0);
endDate = datetime(2021,12,31,23,0,0);
dates = startDate:hours(1):endDate;
Do this once before all loops, because it does not depend on the loop counter n.

3 Comments

Moreover, the only thing that changes in your loop is the Ddays line, you could define your range of days before the loop and loop for all your wanted ranges:
%Create day_range arrays, first column is low value second is high; each
%row is a new range of selected days
Day_range = [1,10; 11, 20; 21, 31]
Day_range = 3×2
1 10 11 20 21 31
for i_day = 1:size(Day_range,1)
for n = 1:12
%% pick each 10 days data
Onemonth = dates.Month==n ;
t = dates(Onemonth) ;
Ddays = t.Day >= Day_range(i_day,1) & t.Day <= Day_range(i_day,2) ;
% Extract data per month
t10days = tempI(:,:,Ddays);
% rotate data
tempII = flipud(rot90(t10days));
% Average data
temp = mean((tempII-273.15),3);
%% Plot
figure(i_day) %new figure for each range
imagescn(lon,lat,temp);
colormap jet
axis image
end
end
Yes thank you. this is the answer i want.
although I do not understand the logic in this line.
Ddays = t.Day >= Day_range(i_day,1) & t.Day <= Day_range(i_day,2) ;
but I have matched and the answer is the same.
and if you have time to answer, why use this
for i_day = 1:size(Day_range,1)
instead of like this
for i_day = 1:3
What is the difference?
Jan
Jan on 29 Jun 2022
Edited: Jan on 29 Jun 2022
Ddays = (t.Day >= Day_range(i_day,1) & t.Day <= Day_range(i_day,2));
This replies a logical vector, which is TRUE, if the element of t.Day is inside the specified range.
for i_day = 1:size(Day_range,1)
Of course, if the corresponding size equal 3, you can write 3 also. But this approach is more general and determines the size dynamically. It is a good programming practice to write flexible code without assumptions about the inputs.
Replace
flipud(rot90(t10days));
by the more direct:
permute(t10days, [2, 1, 3]);
Or even better:
t10days = tempI(:,:,Ddays);
tempII = flipud(rot90(t10days));
temp = mean((tempII-273.15),3);
by the cheaper and simpler:
temp = mean(tempI(:, :, Ddays), 3).' - 273.15;

Sign in to comment.

More Answers (0)

Products

Asked:

Den
on 28 Jun 2022

Edited:

Jan
on 29 Jun 2022

Community Treasure Hunt

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

Start Hunting!