DumpSave for the forgetful
I think you can use Names["Global`*"]
to get the name:
a = RandomReal[{0, 1}, 10];
SetDirectory[$TemporaryDirectory];
DumpSave["1.mx", a];
Quit[]
SetDirectory[$TemporaryDirectory];
<< 1.mx
Names["Global`*"]
(*{"a"}*)
If you use Import/Export
to load/save .mx
files, instead of DumpSave
, then the variable does not get embedded in the file, and you can assign it to any variable of choice in the new session.
x = RandomReal[1, {100, 100}];
Export["~/tmp.mx", x];
y = Import["~/tmp.mx"];
x == y
(* True *)
Method 1: Enable messages for new symbols and load MX file
The following code will load MX file and show a General::newsym
message for each newly created symbol.
On[General::newsym]; (* enable message for each new symbol *)
Off[General::stop]; (* prevent stop after 3 messages *)
Get["file.mx"]; (* load MX file *)
On[General::stop];
Off[General::newsym];
Advantage: This method shows new symbols in all contexts, not only in Global
.
This is useful if you don't know the context of the variables stored in the MX file.
Disadvantage: Only newly created symbols will be reported. If a symbol is already defined in your current Mathematica session and MX file updates/redefines this symbol, then no message will be shown.
Method 2: Load MX file and save new symbols into a List
If you want to save the names of newly created symbols into a list,
you can use the $NewSymbol
handler.
The following function (based on this post by @Leonid Shifrin)
will load an MX file and return a list of newly created symbols including their contexts:
newSymbols[mxfile_] := Module[
{tag},
Block[
{$NewSymbol=Sow[#2<>#1, tag]&},
Reap[Get[mxfile],tag][[2]]
]
]
This method has the same advantages and disadvantages as Method 1.
Method 3: Use auxiliary kernel to inspect the contents of MX file
This method will be helpful if you want to know which symbols are defined by MX symbols, but don't want to load MX file into your current session.
You can start a fresh auxiliary kernel, load MX file there, save a list of newly created symbols, and quit auxiliary kernel.
This algorithm can also be automated:
(* newKernelEvaluate starts a fresh auxiliary kernel, evaluates expr, *)
(* quits auxiliary kernel, and returns the result of evaluation. *)
ClearAll[newKernelEvaluate];
Attributes[newKernelEvaluate] = HoldAll;
newKernelEvaluate[expr_] := Module[
{link, result},
link = LinkLaunch[First@$CommandLine <> " -mathlink -noprompt"];
LinkWrite[link, Unevaluated@EvaluatePacket@expr];
result = LinkRead@link;
LinkClose@link;
Replace[result, ReturnPacket@x_ :> x]
];
ClearAll[getSymbolsDefinedInMX];
getSymbolsDefinedInMX[mxfile_] := Module[
{tag},
With[{mymxfile=mxfile},
newKernelEvaluate[
Block[
{$NewSymbol=Sow[#2<>#1, tag]&},
Reap[Get[mymxfile],tag][[2]]
]
]
]
];
You can now run getSymbolsDefinedInMX["file.mx"]
to get a list of symbols defined in this MX, without actually loading any symbols in the current session.
This method will detect almost all symbols defined by MX. Only redefinitions of built-in symbols will not be detected.
Note
If you spot System`Private`ConvertersPrivateDumpSymbol
among the symbols which are created by your MX file, then this MX file was created using Export["file.mx", someExpression]
(see the answer of R.M. above). In this case you can just use someVariable = Import["file.mx"]
.