how to add to txt file from certain point

1 次查看(过去 30 天)
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?

采纳的回答

Cedric
Cedric 2013-8-5
编辑:Cedric 2013-8-5
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) ;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by