I saved an InterpolationFunction in a ".mx" files using DumpSave on a variable that was scoped by a Module. Here is a stripped-down example:
Module[{interpolation},
interpolation=Interpolation[Range[10]];
DumpSave["interpolation.mx", interpolation];
]
Is there a way to find out the variable name, presumably of the form interpolation$nnn, of the expression when I Get the interpolation? It is not apparent what the variable is when using
<<"interpolation.mx"
Next time I will not use a Module for scoping the save variable, but meantime I'd like to access the saved data and assign it to a new variable.
Answer
You can open the MX file in an ASCII editor. The variable name is there in plain text (interpolation$511 in my case). The rest is binary gibberish.
So given an MX file with a single variable with the $ suffix, the following expression can be used to access that variable directly:
getExpression[filename_] := Module[{a},
ToExpression[(a = StringCases[Import[filename, "Text"],
WordCharacter ... ~~ "$" ~~ NumberString][[1]])];
<< (filename);
ToExpression[a]
]
E.g.,
result = getExpression["interpolation.mx"]
(* InterpolatingFunction[{{1,10}},<>] *)
Evaluating the variable name before reading it with Get (or <<) seemingly overcomes the Temporary attribute discussed in Leonid`s answer. As a (perhaps unwanted) side-effect of the above expression, the original variable remains defined.
This approach could probably be extended to work with multiple variables and expressions in one MX file.
Comments
Post a Comment