data appending in text file

Hi,
I created a text file using MATLAB. For example, 10 by 3 matrix and I saved it as a text using fopen and fprint in particular format. Now I want to append lines (data)to this text, say at row 1, row 5, and row 10, i.e at a fixed interval. How can I do this. Remember, I need to add lots of these lines.
I greatly appreciate any help.
Thanks Rafiq

 Risposta accettata

Cedric
Cedric il 14 Ago 2013
Modificato: Cedric il 14 Ago 2013

0 voti

Instead of opening the file with write permission using 'w', open it in with append permission using 'a'.

7 Commenti

Cedric
Cedric il 14 Ago 2013
Modificato: Cedric il 14 Ago 2013
Sorry, after reading Walter's answer, I realize that I misread the question. For inserting lines, I would build a new file actually while reading the original. For example (not tested), using a random 3x4 array of lines to insert (this assumes that there are >= 9 lines in the data file):
data2insert = rand(3, 4) ;
atLine = [1, 5, 10] ;
lineCnt = 1 ;
insertCnt = 1 ;
fid_in = fopen('data.txt', 'r') ;
fid_out = fopen('data_new.txt', 'w') ;
while ~feof(fid_in)
if lineCnt == atLine(insertCnt)
fprintf(fid_out, '%f %f %f %f\r\n', data2insert(insertCnt,:)) ;
insertCnt = insertCnt + 1 ;
end
line = fgetl(fid_in) ;
fwrite(fid_out, line) ;
lineCnt = lineCnt + 1 ;
end
fclose(fid_in) ;
while insertCnt < length(atLine)
fprintf(fid_out, '%f %f %f %f\r\n', data2insert(insertCnt,:)) ;
insertCnt = insertCnt + 1 ;
end
fclose(fid_out) ;
Dear Cedric, Thank you very much for your response. It looks like I am getting something but not exactly the way I want. I added few lines with your code so that you can see what I want.
B=ones(10,3); fid=fopen('spot.txt','w'); spotformat='%8.3f%8.3f%8.3f\r\n'; fprintf(fid,spotformat, B); fclose(fid);
%So I made a file spot.txt with all entry one using B
data2insert = [3 4 5 6; 7 8 9 10; 11 12 13 14] ;
%I want this data2insert in the spot.txt. In the first line I want to insert 3 4 5 6, in the fifth line, I want to insert 7 8 9 10 and in the tenth line, I want to insert 11 12 13 14.
atLine = [1, 5, 10] ;
lineCnt = 1 ;
insertCnt = 1 ;
fid_in = fopen('spot.txt', 'r') ;
fid_out = fopen('data.txt', 'w') ;
while ~feof(fid_in)
if lineCnt == atLine(insertCnt)
fprintf(fid_out, '%f %f %f %f\r\n', data2insert(insertCnt,:))
insertCnt = insertCnt + 1 ;
end
line = fgetl(fid_in);
fwrite(fid_out, line) ;
lineCnt = lineCnt + 1 ;
end
fclose(fid_in) ;
while insertCnt < length(atLine)
fprintf(fid_out, '%f %f %f %f\r\n', data2insert(insertCnt,:)) ;
insertCnt = insertCnt + 1 ;
end
fclose(fid_out) ;
It looks like everything is coming out but only in three rows. It should be 13 lines (rows) in total. Your help is highly appreciated.
Thanks Rafiq
Cedric
Cedric il 15 Ago 2013
Modificato: Cedric il 15 Ago 2013
Dear Rafiq,
It's my mistake and I can see that now that I am on a computer with MATLAB (and that I can test). Please replace
fwrite(fid_out, line) ;
by
fprintf(fid_out, '%s\r\n', line) ;
The issue was that_FGETL_ is removing the carriage return and new line special characters (escape codes '\r\n'), so we cannot just FWRITE what FGETL is returning, but we have to add them "manually" using FPRINTF.
Cheers,
Cedric
Dear Cedric, Thank you very much for your help. I was able to find the way I want it. I am struggling with few other things. If I increase B ==ones(15,3) and save it as spot.txt (my original text file), it looks like the output text is not reading after eleventh line. I am sorry for my naive questions. I tried to get it around. Also, MATLAB shows boundary limit problem. Could you please look at this. Thanks.
_B=ones(15,3);
fid=fopen('spot.txt','w');
fprintf(fid, ' Ek SigE SigX SigY SigAng VDSX VDSY \n');
spotformat='%8.3f%8.3f%8.3f\r\n';
fprintf(fid,spotformat, B);
fclose(fid);
data2insert = [3 4 5 6 7 8 9 10; 11 12 13 14 15 16 17 18;...
19 20 21 22 23 24 25 26] ;
atLine = [2, 6, 10] ;
lineCnt = 1 ;
insertCnt =1 ;
fid_in = fopen('spot.txt', 'r') ;
fid_out = fopen('data.txt', 'w') ;
while ~feof(fid_in)
if lineCnt == atLine(insertCnt)
fprintf(fid_out, '%8.3f%8.3f%8.3f%8.3f%8.3f%8.3f%8.3f%8.3f\r\n',...
data2insert(insertCnt,:)) ;
insertCnt = insertCnt+1
end
line = fgetl(fid_in);
fprintf(fid_out, '%s\r\n', line)
lineCnt = lineCnt + 1
end
fclose(fid_in) ;
while insertCnt < length(atLine)
fprintf(fid_out, '%8.3f%8.3f%8.3f%8.3f%8.3f%8.3f%8.3f%8.3f\r\n',...
data2insert(insertCnt,:)) ;
insertCnt = insertCnt + 1 ;
end
fclose(fid_out) ;_
Cedric
Cedric il 15 Ago 2013
Modificato: Cedric il 15 Ago 2013
Yes, it's an issue with the following line:
if lineCnt == atLine(insertCnt)
that you can replace with:
if insertCnt <= length(atLine) && lineCnt == atLine(insertCnt)
Other than that, you might want to add a carriage return in the formatSpec of the first call to FPRINTF, as in the following:
fprintf(fid, ' Ek SigE SigX SigY SigAng VDSX VDSY\r\n');
and maybe shift atLine by 1 if it shouldn't take into account the header line(?)
Cheers,
Cedric
Mohammad
Mohammad il 15 Ago 2013
Cedric,
So kind of you. Thanks a lot. This solved my problem and saved me a lot of time. Thanks again.
Rafiq
Cedric
Cedric il 15 Ago 2013
You're welcome!

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 14 Ago 2013

0 voti

The only way to insert lines into a text file is to read in the file and write out the modified file. There is no way to seek directly to a particular line, and there is no way to insert in the middle of a text file.
Depending on what you want to insert into the file, you may wish to use system utilities such as "sed" or "awk" or "perl"

Community Treasure Hunt

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

Start Hunting!

Translated by