How to open two files instead of one, using this "for" loop?
1 view (last 30 days)
Show older comments
Ismail Qeshta
on 16 Feb 2019
Edited: Ismail Qeshta
on 16 Feb 2019
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
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.
Accepted Answer
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
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.
More Answers (1)
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
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?
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!