write in a specific line of text file

Hi all,
I'm writing a code and I would save the result in a specific line of text file. for example, the code should write the result on .txt file from line 6 or 7 but I don't know how can I do it. I've used
fid = fopen('z.txt', 'w');
fprintf(fid, '%d\r\n',MyResult);
fclose(fid)
But it can just write in a new line no each line as I want. Could you please help me
Hamed

4 Comments

You mean you want to append a line? Or should the resulting function be able to replace the line of text? The documentation (type doc fopen) explains how to do the first, the second will be more tricky.
hamed
hamed on 24 Jan 2018
Edited: hamed on 24 Jan 2018
Actually, I have a main .txt file with some headers (from line 1 to 6) and in each time my code generates a matrix and I want to add this data to my text file after line 8 or 9. The header should be unchanged.
If you want to append lines, read the doc. If you want to do something else, you'll have to explain it more, preferably with some examples, because right now, I don't know if I understand you.
ok, let me explain more I have a text file with 6 lines header, for example, name and address of a company
my code creates a matrix (50*5) and I want to add this matrix after line 8.

Sign in to comment.

Answers (1)

Rik
Rik on 24 Jan 2018
Use the append switch in fopen, fprinft an empty line, and fprintf the matrix with the FormatSpec of your choosing.

Categories

Asked:

on 24 Jan 2018

Answered:

Rik
on 24 Jan 2018

Community Treasure Hunt

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

Start Hunting!