MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Formatted Output (Exporting Data)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64455] Re: Formatted Output (Exporting Data)
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Fri, 17 Feb 2006 04:12:43 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On 2/16/06 at 3:05 AM, thenders at gmail.com (tlhiv) wrote:

>f[x_]:=x^2
>Export["foo.d",Table[{x, f[x]}, {x, -1, 1, 0.2}],"Table"]

>This generates a file "foo.d" that look like:
>
>-1   1 -0.8 
>0.6400000000000001 -0.6 

<rest snipped>

>I would very much like to have "foo.d" look like:

>-1 1 
>-0.8 0.64

<rest snipped>

>and with the delimeter being a TAB instead of a SPACE.  In
>particular, the precision used above is not so good.  I would like
>everything rounded off to, say, 6 significant digits.  An analogous
>C style output would be something like

>fprintf(fid,"%6f\t%6f\n,x,f)
>
>and is performed for each "element" of x.  Can this be accomplished
>easily with Mathematica?

Yes. I believe the simplest solution is to use NumberForm to set the desired precision, convert everything to strings then export the data.

For your specific example, that is 2 column data, the following function will do the conversion

format[{x_, y_}, n_] := 
  StringJoin[ToString[NumberForm[x, n]], "\t", ToString[NumberForm[y, n]]]
  

And the file foo.dat can be created with

Export["foo.dat", 
   format[#, 2]&/@Chop[Table[{x, f[x]}, {x, -1, 1, 0.2}]], "Table"]

Notice, I've used Chop here to keep NumberForm from outputing 2 significant digits of some small value that should really be zero. This could have been put in the function format instead.

--
To reply via email subtract one hundred and four


  • Prev by Date: Re: Re: Using a text editor like interface for Mathematica?
  • Next by Date: Re: Set function
  • Previous by thread: Re: Formatted Output (Exporting Data)
  • Next by thread: Re: Formatted Output (Exporting Data)