3
$\begingroup$

I have a complicated function that I want to plot using ListPlot but my computer crashes. I think it runs out of memory.

So, what i do now is to save each iteration in a .txt file.

For example i have Exp(-100x),

Table[ 
 ...
 f = OpenAppend["test.txt"];
 WriteString[f,N[x]," ",N[Exp[-100x]],"\n"];
 Close[f]; ,  {x, 0,1 0, 1/100}]

Now, at $x=0.12$, the value should be $6.14421$x$10^{-6}$ however if I look at the .txt file, i have

0.11 0.0000167017
0.12           -6
6.14421 10
0.13           -6
2.26033 10

How do I fix this?

Thanks in advance

$\endgroup$
0
0
$\begingroup$

one simple trick:

WriteString[f, ExportString[{{N[x], N[Exp[-100 x]]}}, "Table"], "\n"];
$\endgroup$
1
  • $\begingroup$ very simple. thanks! $\endgroup$
    – PhilCsar
    Oct 27 '16 at 4:00
2
$\begingroup$

The problem with exporting numbers in 10^x form is that for typesetting purposes, Mathematica puts the -6 on the line above the base number.

ToString[1.*^-6] // FullForm

" -6\n1. 10"

\n is the new line character.

If you want to export numbers in scientific form, here's a couple ways:

Use base + e + exponent:

NumberForm[1.*^-6, NumberFormat -> (Row[{#1, "e", #3}] &)]

"1.e-6"

Build a custom formatting function:

 customformat[num_] := 
  ToString@NumberForm[num + 0., {10, 5}, NumberFormat -> 
   (Row[{#1, "*10^", If[ToExpression[(#3 /. "" -> "0")] < 0, "-", "+"], 
    StringPadLeft[StringReplace[ToString@#3, "-" -> ""], 3, "0"]}] &)]

customformat[1*^-6]

"1.00000*10^-006"

Then Export to your heart's desire.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.