I have tried to find a solution to this problem in various places and thought I had it solved, but apparently not.
When a Mathematica notebook is closed or Mathematica crashes, a lot of stuff is lost, even if a Save was just done. Its not clear to me what kinds of things are lost but I believe it includes definitions.
I have a large notebook for which some definitions are based on symbolic integrals that take a long time to compute, for example. I think that data can also be lost. Obviously I dont want to re-evaluate the entire notebook every time I open it; in fact, Im not even sure that would work due to the interactive nature of notebooks and the fact that typically one evaluates only part of a notebook and then another part.
Here is what I have tried. I thought it was working in the past but it does not work now. There are two parts: one saves to a .mx file and one reads from the same .mx file. Note that my file path contains spaces.
(* Save to .mx file. *)
mxFileName = StringReplace[NotebookFileName[], ".nb" -> ".mx"];
DumpSave[mxFileName, Evaluate@Context[]];
and
(* Retrieve from saved .mx file. *)
mxFileName = StringReplace[NotebookFileName[], ".nb" -> ".mx"];
Get[mxFileName];
When the retrieve part is executed, I get complaints of multiple contexts and shadowing, such as
x$::shdw: Symbol x$ appears in multiple contexts {Notebook$$21$649972`,Notebook$$29$735513`}; definitions in context Notebook$$21$649972` may shadow or be shadowed by other definitions. >>
I understand that when I re-open the saved .nb file it gets a new context name, in this case Notebook++21+649972-, and by examining the top of the .mx file it is apparent that it is associated with another context, in this case Notebook++29$735513- which is probably the context of the notebook before closing/crashing. [[Note that in this paragraph I replaced the dollar symbol with + and the tick mark with - to prevent weird formatting within this forum post.]] Note that all of my notebook contexts are Unique to this notebook."
I have tried renaming the new context with the old context name, as in
$Context="Notebook$$29$735513`
before running the retrieve code but none of my definitions are restored.
I feel that I am close to making this work and this approach seened to work on small test notebooks. What am I doing wrong?
Jerry