MATLAB: Fprintf in a for loop

for loopfprintfMATLAB

I wrote this function to write all numbers w(i) in a text document.
But as you can see the program only writes the last number w(12) = 2^12 in the document.
What can I do to get all w(i) into the document?
function test
N = 12;
k = 2;
for i = 1:N-1
w(i) = k^i;
w(i)
fid = fopen('Test2.txt', 'w');
fprintf(fid, '%s\n', 'Data w');
fprintf(fid, '%6.4f\t', w(i));
fclose(fid);
end
end
blabla.png

Best Answer

I would do something like this:
N = 12;
k = 2;
for i = 1:N-1
w(i) = k^i;
w(i)
end
fid = fopen('Test2.txt', 'w');
fprintf(fid, '%s\n', 'Data w');
fprintf(fid, '%6.4f\n', w);
fclose(fid);
So save the intermediate results, and print everything to your file at the end.