How can i read in and work on multiple files with the similar name structure but different from each other by one or two characters in their name?

조회 수: 13 (최근 30일)
Hi,
I have a 200 .nc files with name : OCS_iso1.cam.h2.00X-Y.nc with X and Y variables in the names, X=(01:01:100) and Y=(01:01:12).
I was wondering if there's a way to read in the files without loading them one by one and also read information from these files (note that all the arrays in these .NC files have same names) (example: lets say there is an array called lev(24*19*66)) and work with them such as: summing them up or taking average, etc.
thank you

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 8일
"I was wondering if there's a way to read in the files without loading them one by one"
No, you need to read each of them. You can create a loop that names them one by one automatically, however
for this_x = 1:100
for this_y = 1:12
this_file = sprintf('OCS_iso1.cam.h2.00%02d-%02d.nc', this_x, this_y );
data = ncread(this_file, ....);
end
end
  댓글 수: 2
Joseph
Joseph 2018년 3월 8일
편집: Walter Roberson 2018년 3월 8일
Thanks, Walter Roberson
it was exactly what I wanted. I just made a slight change (adding index) to read in all files as below:
for this_x = 1:13
for this_y=1:12
this_file{this_x,this_y}= sprintf('OCS_iso1.cam.h2.00%02d-%02d.nc', this_x, this_y );
data{this_x,this_y}= ncread(this_file{this_x,this_y},...);
end
end;
Walter Roberson
Walter Roberson 2018년 3월 8일
As a matter of programming convention, I use this_* to refer to the one particular value that I am working on now, and I use a more collective name to refer to the collection of names. So in the above if I did not need to know what the file names were later on, then
for this_x = 1:13
for this_y=1:12
this_file = sprintf('OCS_iso1.cam.h2.00%02d-%02d.nc', this_x, this_y );
data{this_x,this_y}= ncread(this_file,...);
end
end
and if I did need to know the file names later on,
for this_x = 1:13
for this_y=1:12
this_file = sprintf('OCS_iso1.cam.h2.00%02d-%02d.nc', this_x, this_y );
filenames{this_x, this_y} = this_file;
data{this_x,this_y}= ncread(this_file,...);
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by