I have a data set (currently a table in a .csv file). I would like to share this notebook with colleagues either as a notebook, or (even better) as a CDF document. Is there a way to embed the data (on the order of a few megabytes) directly into the notebook?

  • $\begingroup$ David Reiss Scientific Arts had a blog some years ago in which he outlined this very thing. However, he seems to have taken this part of his website down. $\endgroup$ – dwa Apr 11 '15 at 4:37
  • $\begingroup$ @dwa I agree with belisarius, therefore I converted your post to a comment. $\endgroup$ – Mr.Wizard Apr 11 '15 at 6:50
  • $\begingroup$ Possible duplicate?: (11891) $\endgroup$ – Mr.Wizard Apr 11 '15 at 6:51

One possibility would be to create a notebook cell containing the imported data in Compressed form:

testData = RandomReal[{0, 1}, {100, 100}];
Export["file.csv", testData];

f = Compress[Import["file.csv"]];
 Cell[BoxData[RowBox[{"mydata", "=", ToBoxes[f], ";"}]], "Input"]]

Here, I created a test file and imported it. I store a compressed version in f. The NotebookWrite command inserts the string contained in f as a new cell in your current notebook. Now you can delete the previous lines and keep only the newly generated cell. It starts with myData =, and assigns the compressed file contents to that symbol. When you send a notebook containing this cell to someone else, they can then just evaluate that cell and get the data by doing Uncompress on the result. In the next two lines, I verify that this works by comparing to the original data:

reconstituted = Uncompress[mydata];
reconstituted == testData

(* ==> True *)
| improve this answer | |
  • $\begingroup$ This does what I had asked originally. Thank you very much for your time in answering. $\endgroup$ – parselmouth Apr 13 '15 at 21:23

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.