I am trying to create a function that saves a set of symbols into "test.mx" and is easy to use.

list = {a,b,c}; f[x_] := x^2; z = 12.45;

In my attempt, something is saved, because the resulting file is not empty, but I cannot retrieve it. I suspect that the original symbols (list,f,z) are evaluated in the moment of passing the arguments to the function, and the symbols are lost.

Following other related posts, I tried a few things with Hold, Evaluate, Unevaluated, ToString, without success. Any comment would be much appreciated.

My attempt:

If[! DirectoryQ[DirectoryName[directory]],
  • $\begingroup$ I think it's a scoping issue, because this works outside of a Module: directory = NotebookDirectory[] <> "/save/"; DumpSave[directory <> "test.mx", {z, f, list}]; SetDirectory[directory]; << test.mx $\endgroup$
    – MelaGo
    Sep 19, 2023 at 14:27
  • 1
    $\begingroup$ Yes it works outside Module. That's why I thought it was relevant enough to create the question :) $\endgroup$
    – Albercoc
    Sep 21, 2023 at 9:09

1 Answer 1


The working function might look like the one below

save3[where_String][what__] := 
  directory = 
   FileNameJoin[{NotebookDirectory[EvaluationNotebook[]], "save"}];
  If[! DirectoryQ[DirectoryName[directory]], 
  Export[FileNameJoin[{directory, where <> ".mx"}], {what}, "MX"]

    list = {a, b, c}; f[x_] := x^2; z = 12.45;
    save3["test"][{list, f, z}]

(* "/home/acus/save/test.mx" *)


    (data = Import["/home/acus/save/test.mx"]) // LeafCount
(* 8 *)

(* {{{a, b, c}, f, 12.45}} *)
  • $\begingroup$ Thank you for the answer! But neither list nor z are defined with this function. Also f[2] remains unevaluated $\endgroup$
    – Albercoc
    Sep 21, 2023 at 9:05
  • $\begingroup$ I use Export to save in Mma memory dump format "MX" only really huge pieces of data (many GB), which takes long time to compute. From my point of view to run definitions and to make assignments takes almost no time, so I never save them. Fresh session is always a best way to continue with necessary data loaded. By the way, the "MX" format is very Mma version and OS system dependent. Only use it for short time projects. The "WDX" format is much more universal, but also a lot slower. I can't help you with DumpSave. $\endgroup$
    – Acus
    Sep 21, 2023 at 10:04

Your Answer

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

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