Writing data to file

Dear all,
I am writing data to a .txt file as follows:
fid = fopen('test.txt', 'wt');
for n = 1:length(z)
for j=1:13
fprintf(fid, 'Si %g %g %g\n', x(j,n), y(j,n),z(j,n));
end
end
fclose(fid);
Which is working lovely - absolutely as intended. The only problem is that this code is contained within another for loop, which resets the old values of x,y,z and puts some new ones in each time the loop interates, so what ends up happening is that I only get the x, y and z values for the last interation of the loop in my file, but I need all of the values.
Is there a way I can get Matlab to start writing the new x,y,z values on a clean line rather than overwriting the old data already in the file? Or perhaps a piece of code I can add to my loop above to implement this another way?
Thanks for any help.

Answers (1)

Walter Roberson
Walter Roberson on 8 Mar 2011

1 vote

If you change the 'wt' to 'at' then the file will be appended to each time.

1 Comment

Jan
Jan on 8 Mar 2011
Simple, exact, exhaustive. +1

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 8 Mar 2011

Community Treasure Hunt

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

Start Hunting!