Error in my "for" loop?

조회 수: 1 (최근 30일)
Ismail Qeshta
Ismail Qeshta 2019년 3월 5일
댓글: Ismail Qeshta 2019년 3월 6일
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

채택된 답변

Bob Thompson
Bob Thompson 2019년 3월 5일
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
Bob Thompson
Bob Thompson 2019년 3월 6일
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.
Ismail Qeshta
Ismail Qeshta 2019년 3월 6일
Hi Bob,
Just to avoid cofusion (because I myself was confused), I had to delete my above comments and post this final one instead.
I actually got the code to work.
Basically, the idea of having additional "for" loops did not really work, as it causes over-writing.
I just kept one "for" loop for the first loop, and just moved the count=0 before the first loop and count = count+1;
fidC= fopen('Case.tcl','w+');
fprintf(fidC, 'set case %d',count);
inside the second loop.
Thank you very much for all your time and help.
Actually, with you I got to learn a lot about coding, especially how to deal with complex nested "for" loops like that.
Thank you very much again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by