Tags: ascii, columns, export, file, firstcolumn, format, matlab, programming, save

Save data as ASCII

On Programmer » Matlab

1,220 words with 1 Comments; publish: Mon, 12 May 2008 11:17:00 GMT; (20078.13, « »)

Hi, There,

I have a problem to export the data as ASCII format.

I need to export the data as a file with three columns. The first

column is for the first variable, and the second column is for the

second variable, and so on. Something like this:

x y z

0.0 2 2.3

0.05 2.3 3.1

0.1 1.2 2.4

0.15 1.6 3.5


When I use the command save('xyz.dat', '-ASCII', 'x', 'y', 'z'), the

values are appended instead of the above format.

I appreciate all help from anybody.


All Comments

Leave a comment...

    • If you have your data in row vectors x, y and z you can easily build

      the file text using sprintf:

      fileText = sprintf('%g %g %g\n', [x, y, z]);

      If you have column vectors, transpose x, y and z before.

      The output may contain something like "3.14159e-010" which might not

      be allowed in your application. In that case, use a different format

      specifer than "%g".

      Now just add the variable names

      [sprintf('x y z\n'), fileText];

      and write to a file:

      fid = fopen('test.txt', 'w');

      fwrite(fid, fileText);


      That's it.


      #1; Mon, 12 May 2008 11:18:00 GMT