Clear Filters
Clear Filters

how to add to txt file from certain point

1 view (last 30 days)
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?

Accepted Answer

Cedric
Cedric on 5 Aug 2013
Edited: Cedric on 5 Aug 2013
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) ;

More Answers (0)

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!