9
$\begingroup$

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}];
$\endgroup$
3
  • 1
    $\begingroup$ hm, works for me. Perhaps you could show the actual code? $\endgroup$
    – user21
    Apr 9, 2013 at 7:31
  • $\begingroup$ @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 $\endgroup$
    – shdrums9
    Apr 9, 2013 at 21:21
  • $\begingroup$ 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. $\endgroup$
    – user21
    Apr 10, 2013 at 6:31

3 Answers 3

13
$\begingroup$

When you use DumpSave, it stores the expression in Notebook$xyz`thedata 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$abc`thedata, but your data is in Notebook$xyz`thedata, 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.

$\endgroup$
1
  • 2
    $\begingroup$ +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. $\endgroup$ Jun 10, 2013 at 23:40
0
$\begingroup$

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$$19$563288`" *)

In the newly opened notebook:

Get["test2.mx"]
Notebook$$19$563288`thedata
$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – shdrums9
    Apr 11, 2013 at 4:55
0
$\begingroup$

Give the variable an explicit context as follows:

Module[{locImport,locFinishTime,locOutputFileName},
..
.
Greg`locExtractor=gHistDataComTickExtractor@locImport;

locOutputFileName=
"C:\\HistDataCom\\mathAUDUSD\\myAUDUSDmathByTick201512.mx";

DumpSave[locOutputFileName,Greg`locExtractor]

.
.]

You can retrieve the DumpSave as follows:

Get["C:\\HistDataCom\\mathAUDUSD\\myAUDUSDmathByTick201512.mx"]

Evaluate@Greg`locExtractor[[1]]
(* {3.65792*10^9, {0.72616, 0.72622}} *)
$\endgroup$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.