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.

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?

share|improve this question
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

3 Answers 3

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>;
data2 = <your data 2>;

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)

share|improve this answer
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;
  {string = "@#$kill me" <> ToString[int]}
  NotebookDelete[Cells[CellTags -> string]]; 
    Defer[{{1 + 1, 2}, {1 + 1, 2}, {1 + 1, 2}} // TableForm], 
    "Output", GeneratedCell -> True, Evaluatable -> True, 
    CellTags -> string]]

getDataHeldComplete[int_] := 
  {string = "@#$kill me" <> ToString[int]}
  MakeExpression @@ {NotebookRead[Cells[CellTags -> string][[1]]][[
     1]], StandardForm}

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

Then evaluate evaluate in a new cell

share|improve this answer

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

enter image description here

share|improve this answer
+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

Your Answer


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.