This is an example of a problem I have retrieving functions from a file I save using ParalleDo.

(*save data to a file in ParallelDo loop*)

myF[x_] := 2 + x^2 + 3 x^3;

fileName = StringTemplate["D:\\mathematica\\file`1`.m"];

  theDeriv = D[myF[x], x];
  Export[fileName[i], Definition@theDeriv],
  {i, 1, 5}

And if I retrieve the function while in this notebook using:

fileName = StringTemplate["D:\\mathematica\\file`1`.m"];
deriv = Import[fileName[1]]

It returns the derivative:

2 x + 9 x^2

However if I first close Mathematica then re-open this notebook and just retrieve the function by first executing Import, it returns what looks like unusable temporary variable names:

2 Notebook$$16$203416`x + 9 Notebook$$16$203416`x^2

If I just use Do this does not happen but I need to run much more complicated code saving many files and would like to do so in parallel. Can someone help me to get this to work in parallel?

Thank you.


1 Answer 1


Try the following. Notice definition for fileName as well as a slightly different function, so that different derivatives can be calculated (one for each file).

myF[x_] := 2 + x^2 + 3 x^3 + 4 x^4 + 5 x^5;
fileName[k_Integer] := 

In a separate cell, do what you are doing: (I am calculating a few derivatives and four kernels are launched on my system to perform these calcs. Five files are created and if already there, these are overwritten. Choose filepath according to your system.)

ParallelDo[theDeriv = D[myF[x], {x, i}];
  Export[fileName[i], Definition@theDeriv], {i, 1, 5}];

After quitting the kernel (or restarting Mma), definitions such as fileName are not there.You can either load a file directly (It is read as an expression):

deriv = Import["L:\\mathematica\\file2.m"]

2 + 18 x + 48 x^2 + 100 x^3

or evaluate fileName again, after which:

deriv = Import[fileName[1]]

2 x + 9 x^2 + 16 x^3 + 25 x^4

  • $\begingroup$ Thanks for that Syed. It does work if I enter it into a new notebook but fails if I add it to my existing notebook. Seems there may be a problem with my existing notebook. I'll look into it further. $\endgroup$
    – josh
    Oct 14, 2021 at 17:10
  • $\begingroup$ Try and isolate the issue and let me know if I can assist you further. Once the file read/write works, it should work with other notebooks. $\endgroup$
    – Syed
    Oct 14, 2021 at 18:31
  • $\begingroup$ Ok thanks. I have isolated it to the Notebook context: If I create and save the data with default context as Global, then I can close Mathematica, bring up the notebook and retrieve the files. However, I've always used "Unique to this notebook" context and if I save the data with that context and restart and try to retrieve the data the variables are in the temporary format as above. I'm using 12.3. $\endgroup$
    – josh
    Oct 14, 2021 at 21:26

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.