simplify my script?
Show older comments
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
Dyuman Joshi
on 28 Jun 2022
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
on 28 Jun 2022
Dyuman Joshi
on 28 Jun 2022
See Jan's answer.
Accepted Answer
More Answers (0)
Categories
Find more on White in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!