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 trying to export a 1000*100 matrix of numbers to a .dat file using Export (I want the numbers to be separated by a tab) and it's very slow, it takes several minutes, whereas when I export to an Excel file it takes a few seconds.

Is there a way to speed this up ?

Also how can I control the number of decimals I want to export ?

Edit
I found the issue, this happens when I export my dat file on a network drive (at work), the export to an Excel file and a dat file must be implemented in different ways that produce different behaviours. Thank you though for the different interesting answers.

share|improve this question
2  
What command are you using to export the data? Exporting to .dat should take seconds rather than minutes. For example on my system, exporting a 1000x100 of random reals to a .dat file takes about 2.6 seconds. –  Heike Feb 23 '12 at 11:11
    
mat = RandomReal[1, {1000, 100}]; Export["test.dat", mat, "TSV"]; // AbsoluteTiming runs in 2.3 seconds here. –  Szabolcs Feb 23 '12 at 12:51

3 Answers 3

I suggest you watch the presentation BigData: Demystifying Large Datasets in Mathematica by Nick Lariviere. He discusses the various possibilities quite clearly.

share|improve this answer

I cannot reproduce your behavior here. For a $1000\times 1000$ it requires only some seconds here

data = Table[N[i/j], {j, 1000}, {i, 1000}];
AbsoluteTiming[
 Export["tmp/matrix.dat", data, "Table", "FieldSeparator" -> {"\t"}]]

(*
  {13.442387, "tmp/matrix.dat"}
*)

If your separator for numbers is always a tab, then clearly the way @Szabolcs pointed out in his comment should be the preferred one.

Export["tmp/matrix.dat", data, "TSV"]

My sample shows, that you can basically use every desired line/field separator you like.

Heike gave the answer to your second question. I would like to point out, that you might lose information because real numbers are usually stored with $MachinePrecision. When you use now SetAccuracy or SetPrecision you cut them off. There is usually only one situation where you want this: When you look at the numbers. I suspect you are looking at a $1000\times 100$ matrix, so why do you want to cut your numbers?

share|improve this answer
    
I wanted to be able to visually quickly check some numbers in the exported file as decimals are not important in my case, and didn't know the proper function to truncate decimals. –  faysou Feb 23 '12 at 17:26

To answer your second question, to control the number of decimals of the exported data you could change the accuracy of your data using SetAccuracy. For example if you only want to export the first two decimals of each entry you could do something like

data = RandomReal[20, {1000, 100}];
Export["test.dat", SetAccuracy[data, 3]]
share|improve this answer
    
Heike, do you know any (reasonable often used) application, where you want to restrict the accuracy beside when you want to look at the numbers? Maybe my answer is not correct, but I cannot think of any. –  halirutan Feb 23 '12 at 16:38
    
@halirutan It could be that the data is the result of manipulation of experimental data. In that case, the calculated data will have a number of significant digits dependent on e.g. the measurement error of the experimental data. Any digits beyond that won't have any meaning so there's no point saving those. –  Heike Feb 23 '12 at 16:57

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.