Writing data to file in Matlab using fprintf

fprintf is used to write data to file.

Example

fileID = fopen('anyFile.txt','w'); % OPEN FILE IN WRITE MODE

x = 0:1:10; % RANGE FROM 1 TO 10
A = [x; x.^2]; % x AND SQUARE OF x

fprintf(fileID,'%6s %12s\n','x','Square of x');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);

Output (anyFile.txt)

     x  Square of x
  0.00   0.00000000
  1.00   1.00000000
  2.00   4.00000000
  3.00   9.00000000
  4.00  16.00000000
  5.00  25.00000000
  6.00  36.00000000
  7.00  49.00000000
  8.00  64.00000000
  9.00  81.00000000
 10.00 100.00000000
Share

You may also like...