I'm a Mathematica newbie and I'm trying to append some data to a file but I am experiencing some weird behaviour when using PutAppend
.
Here is what I am doing:
AbsVal[T_, numofterms_, a_] := Abs[B2[T, numofterms, a]/numint[T, a]][[1]]
For[n = 1, n < 500, n += 5, PutAppend[Flatten[{n, AbsVal[t, n, a]}], "myfile.txt"]]
I'll now give you example of the outputs. For
AbsVal[t, n, a]
I get an output with the following format:
3.090061579944379*10^-416
But in my file I see
{1,
6.634398712795078837941035470191903174478256263992767`15.954589770191005*^-4\
30}
{6, 3.0900615799443788409187442259678172695491469841`15.954589770191005*^-416}
{11, 2.1455743830188223575511135886399984856855`15.954589770191005*^-403}
{16,
2.2729950468097654970709463565988731409032630716722417`15.954589770191005*^-\
391}
{21, 6.0066411663977041046546546698428746195726683`15.954589770191005*^-380}
{26, 5.31062408165803191323563134185348816`15.954589770191005*^-369}
{31,
1.9022430662165668797880573883969431578102581385`15.954589770191005*^-358}
{36, 3.1567662580039551788994831970476421271`15.954589770191005*^-348}
{41,
2.67979361914634680070557971109230909959314472123`15.954589770191005*^-338}
{46, 1.25574842187340348082241748599920258043`15.954589770191005*^-328}
{51, 3.450109407850428362698276923292493910486398948`15.954589770191005*^-319}
{56, 5.8363462554023196163570433861000185804`15.954589770191005*^-310}
{61, 6.330104189030497*^-301}
{66, 4.55439511956804*^-292}
{71, 2.237735694265989*^-283}
{76, 7.698747588846684*^-275}
{81, 1.895566134531356*^-266}
(...)
which I think is very weird because:
- In the first line, there is a linebreak between
1,
and6.6343987(blah blah blah)
- Sometimes I have that weird
`15.954589770191005
term with that weird apostrophe (which I have no idea what it means) - Also sometime I get a backslash
\
when the line gets too long
As you have noticed, this is all very weird to me!!
What I'm aiming for is for a consistent output like the one we see in the last five lines. What I was REALLY aiming for is to have a csv format as output (without the curly brackets)
What am I doing wrong? or what do I need to configure to fix this?
Thank you very much!
Export
.Put
will write Mathematica expressions, and you don't want Mathematica syntax in your output. The usual way to export data is to first generate all of it, thenExport
a whole list in one go. Always do this unless you know why you need to append step by step. If you're new to Mathematica forget aboutFor
completely, as it will be counterproductive to your learning. For procedural loops useDo
instead, orWhile
when needed. But try to avoid procedural loops. The right way to deal with your example is to generate data withTable
, thenExport
. $\endgroup$result = Table[{n, AbsVal[t, n, a]}, {n, 1, 50, 5}]; Export["output.csv", result]
. $\endgroup$