Change Variable Value at each Iteration Based on Value in Array

조회 수: 3 (최근 30일)
Hi all!
I'm currently reading through ten .nc files. I am interested in looking at a particular variable contained within each file, and this variable is of size 1x4 (lat, lon, level, timestep). I have created a for-loop to read through each of my files (which I have compiled in a folder), and the results of my for-loop lead to a 15X15 matrix for each of my ten files (so I end up with a three-dimensional output, which is of size 15x15x10).
However, I just realized that each of my files has a different timestep of interest. In other words, I need to change the value of the timestep each time I go through the for-loop to a value contained in my array, b. Luckily, my files are neatly organized in a folder and correspond numerically to my array, b. So, if I'm reading through the first file in my folder, the first row in array b will tell me which timestep I must use as I read my variable.
I have tried creating a separate for-loop for the values found within my array b, and then plugging that in to the code I have below, but I have been unsuccesful in computing the actual results. Any tips/pointers/suggestions? Thanks in advance! Here's what I'm currently working with...
b = [1; 3; 4; 2; 1; 1; 1; 4; 2; 2]
Folder = 'C:\my\folder\here'
FileList = dir(fullfile(Folder, '*.nc'))
t = zeros(15,15,numel(FileList))
for iFile = 1:numel(FileList)
iFile
name = fullfile(FileList(iFile).folder, FileList(iFile).name)
level = ncread(name, 'level')
for i = 1:numel(level)
i
t(i,:,iFile) = ncread(name, 'T', [548, 130, i, b], [1, 15, 1, 1])
end
end
end

채택된 답변

Michelle De Luna
Michelle De Luna 2021년 4월 18일
After a couple of days, I got the (as expected, pretty easy) solution: if the size of my array is the same length of the files in my folder...
b = [1; 3; 4; 2; 1; 1; 1; 4; 2; 2]
Folder = 'C:\my\folder\here'
FileList = dir(fullfile(Folder, '*.nc'))
t = zeros(15,15,numel(FileList))
for iFile = 1:numel(FileList)
iFile
name = fullfile(FileList(iFile).folder, FileList(iFile).name)
level = ncread(name, 'level')
for i = 1:numel(level)
i
t(i,:,iFile) = ncread(name, 'T', [548, 130, i, b(iFile)], [1, 15, 1, 1])
end
end
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by