How to open two files instead of one, using this "for" loop?

1 view (last 30 days)
Hi,
My current "for" loop opens one file only and writes three lines in it using permurations, for c, s and t.
I need to modify this code to make it ONLY read the values in "s", since the values in "c" and "t" are always zeros. Also, I need to make the loop opens a second file and record each "j".
For example: The first printed file will have:
0 1 0
0 2 0
0 3 0
0 4 0
0 5 0
The second file will have: set case 1, set case 2, set case 3, set case 4, set case 5.
I tried to modify the code, but it does not seem to be working.
close all; clc; clear all;
c= [0 0 0 0 0];
s = [1 2 3 4 5];
t= [0 0 0 0 0];
for i=1:5;
for= j=1:5;
for m=1:5;
line1 =['' num2str(c(i)) ';'];
line2 =['' num2str(s(j)) ';'];
line3 =['' num2str(t(m)) ';'];
fid= fopen('Pulse.tcl','w'); % First file
fprintf(fid,'%s\n',line1);
fprintf(fid,'%s\n',line2);
fprintf(fid,'%s\n',line3);
fid=fclose('all');
line1=['set case 'numer2str(j)',''];
fid=fopen('Case.tcl','w'); % Second file
fprintf(fid,'%s\n',line3);
fid=fclose('all');
end
end
end
  2 Comments
Cris LaPierre
Cris LaPierre on 16 Feb 2019
Is there any particular reason you are doing this in 3 for loops? You are opening and writing to the files 5*5*5 times. Seems a bit inefficient since you already know the values of s and j without needing the for loops.
Ismail Qeshta
Ismail Qeshta on 16 Feb 2019
Hi Cris,
Thank you for your comment.
Actually, I am a little bit confused to be honset. Basically, all I need is to insert values to be printed in a file (called "Pulse.tcl") in the following order:
0 1 0
0 2 0
0 3 0
0 4 0
0 5 0
And for each case, this needs to be printed in a second file (called "Case.tcl"). This file needs to contain the cases for each value. I just need this to work, not necessarily using "fot" loop. The code in my post is just an attempt.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 16 Feb 2019
Edited: Cris LaPierre on 16 Feb 2019
I was thinking going much simpler. Again, unless there is a reason you are making it so complicated, why not just do something like this:
c= [0 0 0 0 0];
s = [1 2 3 4 5];
t= [0 0 0 0 0];
M = [c' s' t'];
dlmwrite('Pulse.tcl',M,'delimiter',' ');
fid = fopen('Case.tcl','w+');
for j = 1:5
fprintf(fid, 'set case %d\n',j);
end
fclose(fid);
  10 Comments
Cris LaPierre
Cris LaPierre on 16 Feb 2019
You still have to define c, s, and t for that last code to run. Notice what is in the fprintf command
fprintf(fidP, '%d\n%d\n%d',c(j), s(j), t(j));
After the first post, I've only put the code you need to modify from the orginal. Perhaps you haven't noticed because the variables are in your workspace?
Type clear in your workspace and they try running your code.
Ismail Qeshta
Ismail Qeshta on 16 Feb 2019
Thank you very much Cris for all your help. Now the code works well.
Thank you again for all your time and effort.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 Feb 2019
Have only a SINGLE for loop instead of three.
close all;
clc;
clear all;
c = [0 0 0 0 0];
s = [1 2 3 4 5];
t = [0 0 0 0 0];
for k = 1 : length(s)
line1 = sprintf('%d;', c(k)); % In case c ever changes from all 0 to something else.
line2 = sprintf('%d;', s(k));
line3 = sprintf('%d;', t(k));
% Create a file.
% If you don't want it to overwrite every time
S% sub attpend instead, use 'at' instead of 'wt'
fid1 = fopen('Pulse.tcl', 'wt'); % First file
fprintf(fid1,'%s\n',line1);
fprintf(fid1,'%s\n',line2);
fprintf(fid1,'%s\n',line3);
fclose(fid1);
line1 = sprintf('set case %d', k);
fid2 = fopen('Case.tcl', 'wt'); % Second file
fprintf(fid2, '%s\n', line3); % Or did you want the new "line1" instead of line 3
fclose(fid2);
end
I'm not really sure what you want for the TCL file. You're computing a new line1 right before it, but writing out line3 instead of the new line1.
Also you're overwriting the files each time in the loop. Why do that? Do you want new files? Or did you want ot append the data to the existing file?
  3 Comments
Ismail Qeshta
Ismail Qeshta on 16 Feb 2019
Edited: Ismail Qeshta on 16 Feb 2019
By the way, I tried the code that you provided above, I found that the value in "Case.tcl" file prints 0.
Image Analyst
Image Analyst on 16 Feb 2019
Again, I can't figure out if you want line1 or line3 written out. With your code I can't figure that out.
And if c and t are always zero, then why are they even arrays at all? Why not just hard code in zeros into the line strings?

Sign in to comment.

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!