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.

Risposte (1)

Walter Roberson
Walter Roberson il 8 Mar 2011

1 voto

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

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Richiesto:

il 8 Mar 2011

Community Treasure Hunt

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

Start Hunting!

Translated by