Double Loop iterations doubt

1 回表示 (過去 30 日間)
Coastal_Eng
Coastal_Eng 2012 年 8 月 24 日
Hello,
I have a doubt about the possibility of having 2 loops going around in the same For. The code that I have is the following:
for i = 0:natts-1
fieldname = netcdf.inqAttName(ncid(j,:), netcdf.getConstant('NC_GLOBAL'),i);
fileinfo.(fieldname) = netcdf.getAtt(ncid,netcdf.getConstant('NC_GLOBAL'), fieldname );
end
And I would like to change the index on the ncid variable every time the initial index goes around 12 times. Is it possible to do such thing ? to have on index going up every time the first one does a 12 iteration loop ?
Thanks Rod

採用された回答

Matt Fig
Matt Fig 2012 年 8 月 24 日
編集済み: Matt Fig 2012 年 8 月 24 日
Is this what you mean?
if ~mod(i,12)
j = j + 1;
end
I am assuming here that j is set outside the loop you show, and is NOT an index of an outer FOR loop. If it is the index of an outer FOR loop, you should instead set it like this:
for j = mod(0:48,12)+1 % Or whatever.
% Inner loop
end
Also, it is not recommended to mask MATLAB functions, such as the i and j function.
  2 件のコメント
Jan
Jan 2012 年 8 月 24 日
I still think, that "i" and "j" should not be functions and even MLint suggests to use the 1i instead to get the imaginary value.
Coastal_Eng
Coastal_Eng 2012 年 8 月 24 日
Tks for the answer,
I put the J there just for clarifying things, I forgot to say it but the ncid should go around also 12 times (12 variables in an array), but it should only pass to the next variable saved in the array when the first loop goes around 12 times:
exaple
natts=[1,1,1]; ncid=[10,20,30];
for i=1:length(natts) fieldname = function (ncid,i) end
when the ncid is 10 it should run 3 times (length of the natts) befores the ncid becomes 20, and then run other 3 times (length of natts) and finally whenis 30 runs the last 3 times.
Is this possible. I'm doing this because i'm working with 12 netCDF files at a time (i don't know if this is wise),and i want to extract the hole info with only one script. Tks
Rod

サインインしてコメントする。

その他の回答 (0 件)

製品

Community Treasure Hunt

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

Start Hunting!

Translated by