You seem to be out of luck (although I will be happy to be proven wrong). This is a rather subtle point, related to the Temporary
attribute and garbage-collection. I will just share a few observations. First, note that after loading of .mx file on a fresh kernel, the variable is not found anywhere, it is not among the living:
<< "interpolation.mx"
names = Flatten[Names[# <> "*"] & /@ Contexts[]];
Flatten@StringCases[names, ___ ~~ "interpolation" ~~ ___]
{"Manipulate`Dump`interpolationToAnimation",
"Manipulate`Dump`interpolationToManipulate"}
Here is one way to make it persist:
Module[{interpolation},
ClearAll[interpolation];
interpolation = Interpolation[Range[10]];
DumpSave["interpolation.mx", interpolation];
]
Now it will be found. By using ClearAll
, I removed the Temporary
attribute from interpolation
. Interestingly, another way to do this is to define a DownValue
rather than an OwnValue
for it:
Module[{interpolation},
interpolation[1] = Interpolation[Range[10]];
DumpSave["interpolation.mx", interpolation];
]
In this case, too, it can be found, since garbage-collection of Module
-generated variables works differently for DownValues
.
I can not fully explain why this affected the code for DumpSave
, which is still inside Module
. One guess I have is that it gets recorded by DumpSave
all right, with all attributes, including Temporary
. Then, when you load it, it probably gets garbage-collected immediately after creation, due to this attribute.
Serializing in this manner using Module
is also bad (although I am myself guilty of having done this, e.g. here) for another reason: Module
-generated variables are only guaranteed to be unique within a single Mathematica session. It is better to use some custom code to generate variables with names guaranteed to be unique for all sessions (which is easy to do. I recommend to read this discussion).
Block
insteadModule
, besides it, the question is interesting, to see how to work with internal module variables. $\endgroup$Export["file.mmaz", Compress[expression], "String"]
andUncompress@Import["file.mmaz", "String"]
for good performance and platform independence. The disadvantage is relatively high memory use, and of course it is still slower than MX. If performance is not an issue (small expressions), just export/import to/from the WDX format. $\endgroup$