MATLAB: How to add to txt file from certain point

looptxt filewrite

Hi, I have a txt file which is a matrix [col1 col2 col3] (n rows) , i want to input a loop i=1:1:7 that will be added as a 4th col. [col1 col2 col3 1] [col1 col2 col3 2] [col1 col2 col3 3] so on until [col1 col2 col3 7] and then repeat itself until the n row, how can i do that?

Best Answer

One way would be the following:
fid_in = fopen('inFile.txt', 'r') ;
fid_out = fopen('outFile.txt', 'w') ;
cnt = 0 ;
while ~feof(fid_in)
if cnt == 7, cnt = 1 ; else cnt = cnt + 1 ; end
fprintf(fid_out, '%s %d\r\n', fgetl(fid_in), cnt) ;
end
fclose(fid_in) ;
fclose(fid_out) ;