Error in my "for" loop?
2 views (last 30 days)
Show older comments
Ismail Qeshta
on 5 Mar 2019
Commented: Ismail Qeshta
on 6 Mar 2019
Hi,
I basically need to run a nested (or series) loop. I need to run the permutations for a and b and run an external software that will use the values in a and b (first loop) and then c, s and t (second loop).
Let me explain that using numbers,
Assume that in the first loop we have:
a = [100 50];
b = [20 40];
And in the second loop we have:
c= [0 0];
s = [150 596]; % this has to be imported from a file. In this example, it is named "result_all.txt".
t= [0 0];
Now, the loop should read the first values of c, s and t in the first loop, i.e., 0,150 and 0. These values should be printed in a file named "Pulse.acc" vertically, i.e., in the following order:
0
150
0
Now, the second loop should start and read the values in a and b for all (four) permutations, i.e., (100 20), (100 40), (50 20) and (50 40). Each reading in the permutation should count the case number.
In a file named "MatParam8.0.tcl", the values of a and b along with their respective case number should be printed as follows for the first value of c, s and t in the first loop:
m1 100
m2 20
Case 1
--
m1 100
m2 40
Case 2
--
m1 50
m2 20
Case 3
--
m1 50
m2 40
Case 4
This should continue for the second values of the first loop.
Hence,
Case 1:
First loop:
0 150 0
Second loop:
100 200
Case 2:
0 150 0
Second loop:
100 40
Case 3:
0 150 0
50 20
Case 4:
0 150 0
50 40
------- The second reading from the first loop starts.
Case 5:
First loop:
0 596 0
Second loop:
100 200
Case 6:
0 596 0
Second loop:
100 40
Case 7:
0 596 0
50 20
Case 8:
0 596 0
50 40
An external file of OpenSees software will read all these inuput and their cases and print the output files accordingly.
I have tried to develop a code for that, but it does not seem to work. The code is shown below.
If you really focus on my code below, you may be able to link my explanation with what I am trying to do. This is because I have run the original nested "for loop" code successfully, but currently I need to do the opposite, i.e., run one value in the first loop and then excute all the permutations in the second loop, and then run the second value in the first loop, and then again excute all the puermutations in the second loop, and so on.
I hope I could explain that clearly, and please feel free to let me know if I need to explain it further.
The code:
close all; clc; clear all;
fin = 'result_all.txt';
s = dlmread(fin,'');
c=[0 0];
t=[0 0];
for m = 1:2;
count = 0; %initialize
fidP = fopen('Pulse.acc','w+');
fidC= fopen('Case.tcl','w+');
fprintf(fidP, '%d\n%d\n%d',c(m), s(m), t(m));
fprintf(fidC, 'set case %d',count);
fclose(fidP);
fclose(fidC);
a = [100 50];
b = [20 40];
count = count+1;
for i=1:2;
for j=1:2;
line1 = ['set m1 ' num2str(a(i)) ';'];
line2 = ['set m1 ' num2str(b(j)) ';'];
fid=fopen('MatParam8.0.tcl','w');
fprintf(fid,'%s\n',line1);
fprintf(fid,'%s\n',line2);
fid=fclose('all');
!OpenSees Model.tcl
end
end
end
0 Comments
Accepted Answer
Bob Thompson
on 5 Mar 2019
Currently your 'first' loop only loops through one set of indices, but you want it to loop through all c, s, and t values individually. You need more loops therefore.
close all; clc; clear all;
fin = 'result_all.txt';
s = dlmread(fin,'');
c=[0 0];
t=[0 0];
for m = 1:2;
for n = 1:2;
for p = 1:2;
count = 0; %initialize
fidP = fopen('Pulse.acc','w+');
fidC= fopen('Case.tcl','w+');
fprintf(fidP, '%d\n%d\n%d',c(m), s(n), t(p));
fprintf(fidC, 'set case %d',count);
fclose(fidP);
fclose(fidC);
a = [100 50];
b = [20 40];
count = count+1;
for i=1:2;
for j=1:2;
line1 = ['set m1 ' num2str(a(i)) ';'];
line2 = ['set m1 ' num2str(b(j)) ';'];
fid=fopen('MatParam8.0.tcl','w');
fprintf(fid,'%s\n',line1);
fprintf(fid,'%s\n',line2);
fid=fclose('all');
!OpenSees Model.tcl
end
end
end
end
end
As always with my answers, it may not be exactly what you need, so feel free to mess around with it as needed.
8 Comments
Bob Thompson
on 6 Mar 2019
Mmm, I forgot to mention you need to move the 'count = 0' in front of the m loop. It is over writing because it resets to zero each time the p loop begins again.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!