Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm having issues while exporting a tables of numerical values to "*.dat" data files.

Here's the thing, sometimes when I export a table, I get more decimals on some random values. The problem is that they weren't even there in the first place.

Now, for the sake of illustration, here's a very simple example on the issue. I tried constructing the following table :

Table[t, {t, 0, 0.4, 0.1}]

Which provides with the following list :

{0., 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, \
0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, \
0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3, 0.31, 0.32, 0.33, \
0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4}

Now, I just tried to export it using the Exportcommand like this :

Export["t.dat", Table[t, {t, 0, 0.4, 0.1}], "Table"]

and everything should be fine, but when you try to read what's in "t.dat" using FilePrint :

FilePrint["t.dat"]

I get the following :

0. 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35000000000000003 0.36 0.37 0.38 0.39 0.4

Note that the number 0.35 suddenly became 0.35000000000000003, which messes everything up. Is there something to solve this issue? I tried using the NumberForm function but it changes the structure of the exported list...

What I intend to do is to export t list presented above, alongside with another list in which I do operations using the values of the first one. For example :

time = Table[NumberForm[i, 6], {i, 0, 0.2, 0.01}];
op = Table[time[[i]]^2 + time[[i]], {i, 1, Length@time}];
Export["data.dat",Transpose@{time,op}, "Table"];
FilePrint["data.dat"];

Which returns :

0.  NumberForm[0., 6] + NumberForm[0., 6]^2
0.01    NumberForm[0.01, 6] + NumberForm[0.01, 6]^2
0.02    NumberForm[0.02, 6] + NumberForm[0.02, 6]^2
0.03    NumberForm[0.03, 6] + NumberForm[0.03, 6]^2
0.04    NumberForm[0.04, 6] + NumberForm[0.04, 6]^2
0.05    NumberForm[0.05, 6] + NumberForm[0.05, 6]^2
0.06    NumberForm[0.06, 6] + NumberForm[0.06, 6]^2
0.07    NumberForm[0.07, 6] + NumberForm[0.07, 6]^2
0.08    NumberForm[0.08, 6] + NumberForm[0.08, 6]^2
0.09    NumberForm[0.09, 6] + NumberForm[0.09, 6]^2
0.1 NumberForm[0.1, 6] + NumberForm[0.1, 6]^2
0.11    NumberForm[0.11, 6] + NumberForm[0.11, 6]^2
0.12    NumberForm[0.12, 6] + NumberForm[0.12, 6]^2
0.13    NumberForm[0.13, 6] + NumberForm[0.13, 6]^2
0.14    NumberForm[0.14, 6] + NumberForm[0.14, 6]^2
0.15    NumberForm[0.15, 6] + NumberForm[0.15, 6]^2
0.16    NumberForm[0.16, 6] + NumberForm[0.16, 6]^2
0.17    NumberForm[0.17, 6] + NumberForm[0.17, 6]^2
0.18    NumberForm[0.18, 6] + NumberForm[0.18, 6]^2
0.19    NumberForm[0.19, 6] + NumberForm[0.19, 6]^2

What I mean by "changes its structure" is the fact that I can no longer operate on the time list and its values correctly...

share|improve this question
    
Some useful background reading. –  cormullion Feb 27 '13 at 17:56
    
@cormullion Reading it... –  jrojasqu Feb 27 '13 at 18:02
    
Related: mathematica.stackexchange.com/q/13848/121 –  Mr.Wizard Feb 27 '13 at 18:35
    
@Mr.Wizard Thanks, but in fact, I just found the answer to my problem... I could post the answer, but it is so trivial that I doubt it can be helpful to anybody... Should I close/delete my question? If yes, how do I do it? –  jrojasqu Feb 27 '13 at 18:41
    
Post it anyway, if you like. You can't close your own question, but you can delete it. My suggestion is to post your answer and then we will close it as too localized. –  Verbeia Feb 28 '13 at 10:22
add comment

2 Answers 2

up vote 2 down vote accepted

Well, as it turns out, the answer to my question was trivial...

Instead of using NumberForm on the time list to operate, I just needed to export using another NumberForm version of the time list to operate... like time2... pretty easy...

time = Table[i, {i, 0, 0.4, 0.01}];
time2 = Table[NumberForm[i, 6], {i, 0, 0.4, 0.01}];
op = Table[time[[i]]^2 + time[[i]], {i, 1, Length@time}];
Export["data.dat", Transpose@{time2, op}, "Table"];
FilePrint["data.dat"];

which finally gives what I needed :

    0.  0.
0.01    0.0101
0.02    0.0204
0.03    0.0309
0.04    0.0416
0.05    0.0525
0.06    0.0636
0.07    0.0749
0.08    0.0864
0.09    0.0981
0.1 0.11
0.11    0.1221
0.12    0.1344
0.13    0.1469
0.14    0.1596
0.15    0.1725
0.16    0.1856
0.17    0.1989
0.18    0.2124
0.19    0.2261
0.2 0.24
0.21    0.2541
0.22    0.2684
0.23    0.2829
0.24    0.2976
0.25    0.3125
0.26    0.3276
0.27    0.3429
0.28    0.3584
0.29    0.3741
0.3 0.39
0.31    0.4061
0.32    0.4224
0.33    0.4389
0.34    0.4556
0.35    0.4725
0.36    0.4896
0.37    0.5069
0.38    0.5244
0.39    0.5421
0.4 0.56
share|improve this answer
add comment

NumberForm seems fine for the example you give:-

Export["t1.dat", Table[t, {t, 0.3, 0.4, 0.01}], "Table"];
Export["t2.dat", Table[NumberForm[t, 2], {t, 0.3, 0.4, 0.01}], "Table"];
TableForm[ReadList["!diff t1.dat t2.dat", "String"]]

(*
4,5c4,5
< 0.32999999999999996
< 0.33999999999999997
---
> 0.33
> 0.34
*)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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