# Storing data in the Notebook in readable form

The common problem in my theretical work is a need to import some experimental data to work with. This means that my notebook depends on some external files which makes it difficult to share it with others and inconvenient when I return to it after time. These data are generally very much smaller than the notebook I get after analysing it.

What I wish is to have a way to store the data inside the notebook in collapsable cells in order to have an ability to view the data in tablular form and to re-import the data file manually if I change it.

Generally, the data inside of the notebook should look as on the sceenshot:

The output cells are regenerated by pressing Shift + Enter when the data files change. What I need is a way to retrieve the data sets from the output cells in the section "The data cells". Something like this:

dataSet1 = getData["Data set 1"];
dataSet2 = getData["Data set 2"];


where "Data set 1" and "Data set 2" are the text strings in the Subsubsection cells from the section "The data cells".

How to achieve this?

-
No finished product, but I´d use a Button to import the data and generate an assignment cell that is output below that. –  Yves Klett Apr 11 '13 at 7:10
I don't understand very well. You have written "The output cells are regenerated manually when the data files change". Do you confirm the word "manually" or is it a error ? –  andre Apr 11 '13 at 12:24
@andre Yes, the output cells with the data will be generated manually by evaluating the above "Input" cells with the Import statements as showed on the screenshot. –  Alexey Popkov Apr 11 '13 at 12:58
@andre Probably the word "manually" is not the best choice. I just mean that the cells with Import statements must not be Initialization cells because I wish do not depend on the external files. At the same time, if the external files are changed it should be possible to regenerate the cells with the data by Importing the external files. So the output cells are generated automatically by Mathematica but manually in the sence that they are not Initialization cells. Sorry for confusion. –  Alexey Popkov Apr 11 '13 at 13:05

I use buttons for this purpose. Imagine that your data is stored in a variable called data. Then you can do the following:

With[{i = Compress[data]},
Button["Retrieve Data", data = Uncompress[i];
Print[Style["Data has successfully been loaded into variable \"data\"",Blue,Italic,14]]]
]


This creates a button. When pressed, data is populated with your original data.

A warning: If you have a separate variable data already as part of your notebook, it will be overwritten.

An alternative:

Create initiialization cells somewhere in your notebook containing

data1 = <your data 1>;


To save space you can also do

data1 = Uncompress[Compress[<your data 1>]];


Then, highlight Compress[<your data 1>] and evaluate it in place (Ctrl-Shift-Enter)

-
You might also mention that the data is stored across sessions because of the way Button works. –  Yves Klett Apr 11 '13 at 11:05
@YvesKlett That's right. In fact, you can select the cell containing the button, and press Ctrl-Shift-E to see the underlying Cell code. you will see the (compressed version of) your data there. That's the reason why you need the With[...] construct, without it, it would not work. –  Thomas Apr 11 '13 at 11:11
@Thomas As I stated in the question, I wish to store data in readable form, as tables. I need not to compress it since the data have small size. –  Alexey Popkov Apr 11 '13 at 11:56
@AlexeyPopkov you can simply print out data as well within the same With. –  Yves Klett Apr 11 '13 at 13:04
I know it but the goal is to store data in obvious and readable form, not just to display them. And at the same time the cells with the data should be generated by Importing external files. –  Alexey Popkov Apr 11 '13 at 13:16

I put in a defer because of your question here. Maybe you have symbols in your data and this will be useful, who knows. getData succesfully gets the data from the generated cell.

int = 1;
With[
{string = "@#$kill me" <> ToString[int]} , NotebookDelete[Cells[CellTags -> string]]; CellPrint[ ExpressionCell[ Defer[{{1 + 1, 2}, {1 + 1, 2}, {1 + 1, 2}} // TableForm], "Output", GeneratedCell -> True, Evaluatable -> True, CellTags -> string]] ]; getDataHeldComplete[int_] := With[ {string = "@#$kill me" <> ToString[int]}
,
1]], StandardForm}
]

getData[int_] := getDataHeldComplete[1][[1]];


Then evaluate evaluate in a new cell

getDataHeldComplete[1]
data=getData[1]

-

Are you aware that you can write data= before your table and evaluate ? :

-
+1 Interesting idea. As I said, the data cells should be generated automatically. It is possible to automatically generate Initialization cells with the Set expressions like you showed but it is not as straightforward as I would wish. –  Alexey Popkov Apr 11 '13 at 12:13