40
$\begingroup$

A few weeks ago I created a big matrix, and in order not to have to recreate it, I stored it using DumpSave["file.mx", variable]. Now I want to read it back in and so I use <<file.mx. It appears to work fine and to load the file (which takes a few moments because it is 54 MB in size). Now the problem: I have forgotten what I called the matrix, that is, what name variable I used in when I saved it. My generating command is unfortunately not around any more. Is there any way of figuring out what my variable was called or more directly, how to access my data now that it is loaded?

$\endgroup$
6
  • $\begingroup$ I suggest that you also save the notebook you had been working on, and that you use this same notebook to store and load the DumpSave. That way you can resume your workflow exactly where you left off, and you won't have to remember the name of your variables. $\endgroup$
    – a06e
    May 11, 2013 at 5:50
  • $\begingroup$ @becko I do actually have the notebook, but I saved a number of things at different times and evidently erased the particular DumpSave that I ended up needing. Your point is well taken: be more careful! $\endgroup$
    – bill s
    May 11, 2013 at 6:16
  • $\begingroup$ Related: (2900) $\endgroup$
    – Mr.Wizard
    May 11, 2013 at 7:55
  • $\begingroup$ @Mr. Wizard - My problem was actually simpler than in the link, and xsLittlegrass's suggestion of looking at all variables solved it nicely. I did try opening in a text editor, but the file is 54 MB and I wasn't able to spot the right variable name -- presumably it is there somewhere. $\endgroup$
    – bill s
    May 11, 2013 at 8:02
  • 3
    $\begingroup$ The real solution here is rm's answer. DumpSave gets suggested much too often on this site when it's really cumbersome, exactly for the reason you describe. Don't use DumpSave unless you really need to. Just use Export instead. $\endgroup$
    – Szabolcs
    May 11, 2013 at 13:58

3 Answers 3

45
$\begingroup$

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"}*)
$\endgroup$
1
  • 2
    $\begingroup$ Hey great! I can now see that my matrix was called uAll. How could I have forgotten that? Thanks xslittlegrass. $\endgroup$
    – bill s
    May 11, 2013 at 5:31
28
$\begingroup$

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 *)
$\endgroup$
3
  • 3
    $\begingroup$ +1 In many situations this can be a more organized way to do it. $\endgroup$
    – a06e
    May 11, 2013 at 16:48
  • $\begingroup$ @rm -rf I'm torn... I want to accept both answers: xslittlegrass's because it solves my immediate problem, and yours because it's a better way to work, which I will follow in the future. $\endgroup$
    – bill s
    May 12, 2013 at 14:52
  • 1
    $\begingroup$ @bill Since you were apparently not familiar with this method you might also learn something form this summary I put together: mathematica.stackexchange.com/a/2008/121 $\endgroup$
    – Mr.Wizard
    May 12, 2013 at 18:39
15
$\begingroup$

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"].

$\endgroup$

Your Answer

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

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