for loop with multiple indices
Mostrar comentarios más antiguos
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 comentarios
Bob Thompson
el 1 de Nov. de 2018
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
el 2 de Nov. de 2018
Helge Svarstad
el 2 de Nov. de 2018
Respuestas (1)
Voss
el 2 de En. de 2024
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
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!