for loop - Writing specific lines from one txt file to another using Matlab -
given loop, want write specific lines 1 text file using matlab. solutions i've seen mention putting text file array/matrix , writing line line , reading line line (whereas want read specific lines) reformat info (ex. adding ' ' strings , [] ints). below vague thought of code: textfile & othertextfile = files want write to, info = specific line of txt file, = line of text file.
for = 1:100 if < 15 fprintf(textfile, data); else fprintf(othertextfile, data); end end
thanks much in advance!
edit: wasn't clear, actual code needs write several different specific sections, ex. lines 1-15, 60-70 copied 1 file, other lines file. using below accepted answer, utilize exact implementation , maintain putting fprintf statements between if/elseif/else statements.
a text file stream of bytes, meaning there's no explicit info stored in text file saying "the line number n starts @ that position". so, file open, first read line first, sec read line sec , on.
some code illustrate:
% named numbers n_lines = 100 n_switch = 15 % open files fin = fopen('inputfile.txt', 'r'); fout1 = fopen('outfile1.txt', 'w'); fout2 = fopen('outfile2.txt', 'w'); % split lines between 2 files = 1:n_lines % read input info = fgets(fin); if feof(fin) break; end; % write output if < n_switch fprintf(fout1, '%s', data); else fprintf(fout2, '%s', data); end; end; % close files fclose(fin); fclose(fout1); fclose(fout2);
matlab for-loop text
No comments:
Post a Comment