MATLAB: How to apply fprinf for matrix

using fprinf for matrix

I would like to use fprintf for Writing data to a text file. I want to apply fprintf on the following matrix. I have a matrix with n rows and 3 columns. please help with this issue. thanks a lot. for instance.
0.0000 0.0000 0.0000
0.0000 0.1000 0.0000
0.0643 0.0675 0.0000
0.1000 0.0000 0.0000
0.0000 0.2000 0.0000
0.2000 0.0000 0.0000
0.0725 0.1500 0.0000
0.1488 0.0873 0.0000
0.0726 0.2501 0.0000
0.0000 0.3000 0.0000
0.0000 0.4000 0.0000
0.3000 0.0000 0.0000
0.4000 0.0000 0.0000

Best Answer

This is MATLAB, so there really is no point in using an ugly loop when you can do it much faster and simpler with just one csvwrite call:
csvwrite('test.csv',M)
or with one fprintf call (no loop is required!):
M = [...
0.0000,0.0000,0.0000;...
0.0000,0.1000,0.0000;...
0.0643,0.0675,0.0000;...
0.1000,0.0000,0.0000;...
0.0000,0.2000,0.0000;...
0.2000,0.0000,0.0000;...
0.0725,0.1500,0.0000;...
0.1488,0.0873,0.0000;...
0.0726,0.2501,0.0000;...
0.0000,0.3000,0.0000;...
0.0000,0.4000,0.0000;...
0.3000,0.0000,0.0000;...
0.4000,0.0000,0.0000];
%
fid = fopen('test.csv','wt');
fprintf(fid,'%f,%f,%f\n',M.')
fclose(fid);