# How to export/import function values [duplicate]

I have a function with two variables:

f[x_,y_]:=f[x,y] = ...


I calculated some values (they are fractions like 435345345/3424242424) and would like to store the values from x=1 to x=5000 with y=0 in a file, so that I can use them later again in a function g[x]

I exported them:

Export["file.dat", Table[f[x,0],{x,1,5000}]


And then I import them in another file:

data = Import["file.dat", "List"]


Now I can access the fractions in data by using, for example:

ToExpression[data[[7]]]


Which gives me the fraction that was originally stored in f[7,0]

So far so good, but I would again like to have the values stored in some function g, with the properties that (in new file) g[x] = f[x,0] (in old file) for x between 1 and 5000

I am not really sure how the notation for that would be :-(

Thank you for your help.

• Have a look at DumpSave. Feb 19, 2013 at 10:23

You are looking for Save or DumpSave

Here is an example:

(*Put some definitions into f*)
ClearAll[f]
f[x_, y_] := f[x, y] = x + y;
Table[f[x, y], {x, 0, 10}, {y, 0, 10}];

(*are the definitions there?*)
?f

(*Save to file*)
Save["f.m", f]

(*Clear all definitions and check that none remain*)
ClearAll@f
?f

(*Import again*)
Import["f.m"];

?f

• Thank you for your response. How would I save only a specific part of when doing Save["f.m", f], because all data might be big (because the amount of ram mathematica is using after storing the values is 100gb) - I only need f[1,0] to f[5000,0], which is like 40mb when exporting it as a list. Feb 19, 2013 at 11:04
• you can unset the rest as per this question mathematica.stackexchange.com/questions/71/… Feb 19, 2013 at 11:27
• (clicked too early, 1 sec..) Feb 19, 2013 at 12:19
• I did it by basically doing it the other way around, by creating, saving and importing a new function g with just the needed values: For[i = 1, i <= 5000, i++, g[i] = f[i, 0]] -- Save["g.m",g] -- Import[g] That worked, thanks again :-) Feb 19, 2013 at 12:36
• You're welcome. Feb 19, 2013 at 12:37