3
$\begingroup$

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; save["test"][list,f,z] 

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:

Clear[save] save[where_][what__]:=Module[{directory,symbols}, directory=NotebookDirectory[]<>"/save/"<>where<>".mx"; If[! DirectoryQ[DirectoryName[directory]], CreateDirectory[DirectoryName[directory]]]; symbols={what}; DumpSave[directory,symbols];] 
$\endgroup$
3
  • $\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$ Commented 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$ Commented Sep 21, 2023 at 9:09
  • $\begingroup$ I guess you may try creating a package. reference.wolfram.com/workbench/index.jsp?topic=/… $\endgroup$ Commented Feb 10 at 20:24

1 Answer 1

0
$\begingroup$

The working function might look like the one below

Clear[save3] save3[where_String][what__] := Module[{directory}, directory = FileNameJoin[{NotebookDirectory[EvaluationNotebook[]], "save"}]; If[! DirectoryQ[DirectoryName[directory]], CreateDirectory[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" *) 

Then

 (data = Import["/home/acus/save/test.mx"]) // LeafCount (* 8 *) data (* {{{a, b, c}, f, 12.45}} *) 
$\endgroup$
2
  • $\begingroup$ Thank you for the answer! But neither list nor z are defined with this function. Also f[2] remains unevaluated $\endgroup$ Commented 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$ Commented Sep 21, 2023 at 10:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.