# Why doesn't DumpSave work for elements created within a local notebook context?

I recently noticed that when dealing with notebooks with local context, I cannot successfully DumpSave variables, or, rather, when I call Get on the non-empty .mx files, the notebook does not recognize the variables.

As a test, I created two notebooks, one with a global context and the other with a local context, that is, for the latter notebook, I ran the command:

SetOptions[EvaluationNotebook[], CellContext->Notebook];


In both notebooks, I defined variables that contained a list of points from a standard NormalDistribution and plotted them using ListPlot. I used

DumpSave["filename.mx", {datavariable,plotvariable}];


to preserve both the data and plot. Long story short, after I save and close both notebooks and restart Mathematica , neither notebook recognizes the variables used for the data or plot, but after using Get in the global notebook, the notebook does recognize the old variables.

Furthermore, when I run a DumpSave command from the 'local' notebook that saves the entire context, i.e. DumpSave["filename.mx", Evaluate@Context[]], I do get back the definitions. This doesn't solve my problem though, as I want to preserve only a few variables in the code that I am working on: saving the entire context, or, more specifically, all of the definitions associated with the context, would require too much memory for an application I have in mind.

Are there any solutions using DumpSave or should I resort to other methods?

Update, I've pasted the exact code below:

SetOptions[EvaluationNotebook[], CellContext -> Notebook];
distribution = NormalDistribution[];
thedata = RandomVariate[distribution, 1000];
theplot = ListPlot@thedata;
DumpSave["test2.mx", {thedata, theplot}];

-
hm, works for me. Perhaps you could show the actual code? –  user21 Apr 9 '13 at 7:31
@ruebenko, here is the code that isn't working for me: SetOptions[EvaluationNotebook[], CellContext -> Notebook]; distribution = NormalDistribution[]; thedata = RandomVariate[distribution, 1000]; theplot = ListPlot@thedata; DumpSave["test2.mx", {thedata, theplot}]; When I close the notebook, and Mathematica, then relaunch everything and call Get on the test2.mx file, it doesn't recognize thedata or theplot –  shdrums9 Apr 9 '13 at 21:21
you may want to post the code into the question; I think it's an integral part of it and it spares people the time to think of an example. –  user21 Apr 10 '13 at 6:31

## 2 Answers

When you use DumpSave, it stores the expression in Notebook$xyzthedata where the $xyz part is a unique context for that notebook. When you load the file using Get, it restores the expression to the variable in that context.

However, there is no guarantee that your notebook will have the same unique context in two different sessions. Your new thedata might actually be Notebook$abcthedata, but your data is in Notebook$xyzthedata, which is why you don't "see" it (but it's there if you access it with the full context).

A better alternative would be to use Import/Export to save your .mx files as I show in this answer. This way, the variable name is not stored, and thus won't be loaded into an arbitrary context. You can easily assign the data to a new variable in your current context.

-
+1. A side note is that Import / Export can be much slower than DumpSave / Get for .mx files (at least IIRC), so everything comes at a price. This may or may not matter, depending on whether or not the problem is IO-bound. The natural way for .mx files to work seems to attach values to some symbols. –  Leonid Shifrin Jun 10 '13 at 23:40

For me this works as expected, but perhaps the issue that not the proper context is used to retrieve the data:

SetOptions[EvaluationNotebook[],
CellContext -> Notebook]; distribution =
NormalDistribution[]; thedata =
RandomVariate[distribution, 1000]; theplot =
ListPlot@thedata; DumpSave["test2.mx", {thedata, theplot}];
Context[thedata]
(*"Notebook$$19563288" *)  In the newly opened notebook: Get["test2.mx"] Notebook$$19\$563288thedata

-
Thanks for your suggestions and help--I've posted the code in the question, as per your recommendation. I also get the right context, but the variable thedata is itself empty; and I'm still unsure as to why. I.e. evaluating the cell containing only thedata I get back "thedata," not the 1000 values that were stored in thedata` previously. –  shdrums9 Apr 11 '13 at 4:55