simplify my script?
이전 댓글 표시
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
Dyuman Joshi
2022년 6월 28일
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
2022년 6월 28일
Dyuman Joshi
2022년 6월 28일
See Jan's answer.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 White에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!