for loop in file

1 view (last 30 days)
Hi , I have problem with making for loop
I have 01~12 files and contain adt. I going to mean each month of adt and mean 1~12, annual adt
Here is my code and please advise any idea
myFolder='C:\Users\yki\2000\01';
filepattern=fullfile(myFolder,'*.nc');
filelist=dir(filepattern);
nFiles=length(filelist);
for i=1:nFiles
filename1= fullfile(filelist(i).folder,filelist(i).name);
ncdisp(filename1);
adt1=ncread(filename1,'adt');
end
adt1=mean(adt1,nFiles);
myFolder='C:\Users\yki\2000\02';
filepattern=fullfile(myFolder,'*.nc');
filelist=dir(filepattern);
nFiles=length(filelist);
for i=1:nFiles
filename2= fullfile(filelist(i).folder,filelist(i).name);
ncdisp(filename2);
adt2=ncread(filename2,'adt');
end
adt2=mean(adt2,nFiles);
myFolder='C:\Users\yki\2000\03';
filepattern=fullfile(myFolder,'*.nc');
filelist=dir(filepattern);
nFiles=length(filelist);
for i=1:nFiles
filename3= fullfile(filelist(i).folder,filelist(i).name);
ncdisp(filename3);
adt3=ncread(filename3,'adt');
end
adt3=mean(adt3,nFiles);
myFolder='C:\Users\yki\2000\04';
filepattern=fullfile(myFolder,'*.nc');
filelist=dir(filepattern);
nFiles=length(filelist);
for i=1:nFiles
filename4= fullfile(filelist(i).folder,filelist(i).name);
ncdisp(filename4);
adt4=ncread(filename4,'adt');
end
adt4=mean(adt4,nFiles);
  1 Comment
Jan
Jan on 7 Oct 2021
What exactly is your question?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 7 Oct 2021
I answered this in your duplicate question. You need to sum up the output of ncread() into a variable, then after the loop divide the sum by the number of files.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by