for loop with multiple indices
이전 댓글 표시
Hi
I'm trying to write a script which takes an existing data file, changes some of the variables within that file based on input from the user, and finally saves a number of new data files. The number of new files will depend on the number of changes.
The code below works as it is now for creating new files where the Seed number changes automatically. But in order to make a new data file whit a new value for Hs or Tp I need to make a new for loop and change the value for Hs or Tp manually. That is ok for a low number of Hs or Tp, but I need to be able to have around 20 values for Hs and 10 values for Tp, as well as 100 values for Seed. So the number of new files created would be 20*10*100=20000.
% Build new model and access existing data file
model = ofxModel;
model.LoadData('HD35 Deployment.dat')
% Access objects in the data file
environment = model.environment;
% Input data
nseed = input('Specify number of seeds for each wave condition: ');
Hs_start = input('Specify lowest wave Hs (m): ');
Hs_end = input('Specify highest wave Hs (m): ');
Hs = (Hs_start:Hs_end);
Tp_start = input('Specify lowest wave period Tp(s): ');
Tp_end = input('Specify highest wave period Tp(s): ');
Tp = (Tp_start:Tp_end);
% Create data files
for Seed = 1:nseed
environment.WaveTrainHs = Hs(1);
environment.WavetrainTp = Tp(1);
environment.WaveTrainSeed = Seed;
Case = sprintf('Case%d.dat',Seed);
model.SaveData(Case)
end
So if the input is:
Hs_start=1 Hs_end=2
Tp_start=1 Tp_end=2
Seed=2
I would then have a total of eight possible combinations and I would like to get eight new files called Case1, Case2, Case3 etc. where:
- Case1: Hs=1 Tp=1 Seed=1
- Case2: Hs=1 Tp=1 Seed=2
- Case3: Hs=1 Tp=2 Seed=1
- Case4: Hs=1 Tp=2 Seed=2
- Case5: Hs=2 Tp=1 Seed=1
- Case6: Hs=2 Tp=1 Seed=2
- Case7: Hs=2 Tp=2 Seed=1
- Case8: Hs=2 Tp=2 Seed=2
Any suggestions on how to do this?
Thanks
댓글 수: 3
Bob Thompson
2018년 11월 1일
You should be able to input arrays into Hs and Tp input commands. Then set up a series of for loops to evaluate the different options. You can't do multiple indices for a single for loop, but you can nest multiple loops together. Let me know if you want a sample of what I'm thinking of.
Helge Svarstad
2018년 11월 2일
Helge Svarstad
2018년 11월 2일
답변 (1개)
Voss
2024년 1월 2일
case_count = 0;
for Hs=Hs_start:Hs_end
environment.WaveTrainHs = Hs;
for Tp=Tp_start:Tp_end
environment.WavetrainTp = Tp;
for Seed = 1:nseed
environment.WaveTrainSeed = Seed;
case_count = case_count+1;
Case = sprintf('Case%d.dat',case_count);
model.SaveData(Case);
end
end
end
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!