# Save variables values and definition

I used to do a lot of programming in R and recently switched to Mathematica. One thing that bothers me is that I haven't found a way to save variables, their values and definition!

Let's look at an example:

idx = Partition[RandomSample[Range[20], 20], 3];
Table[Subscript[vars, idx[[i]]] = Total[idx[[i]]], {i, 1,
Length[idx]}];
Subscript[vars,{9,15,8}]
(*32*)


Is there a way to save the variable names as well as the corresponding values. And how would I read them into a new file? So that in a new file I can access with Subscript[vars,{9,15,8}] the value.

I tried it with Table[...]>>values.mx and read it in with values=<<values.mx but that only gives me the values, whereas I also need the definitions. I also tried:

DumpSave["values.mx",
Table[Subscript[vars, idx[[i]]] = Total[idx[[i]]], {i, 1, Length[idx]}]]


but got an error

DumpSave::bsnosym: "Table[..] is not defined as a symbol or a context"

-
Does DumpSave[ ] not work properly for you? –  bill s Mar 15 '13 at 8:22
@bills In the last sentence the OP said that he can't get the proper syntax for DumpSave[]. Can you help him? –  belisarius Mar 15 '13 at 8:50
yes, I didn't geht the syntax right, I tried DumpSave["values.mx", Table[Subscript[vars, idx[[i]]] = Total[idx[[i]]], {i, 1, Length[idx]}]] but got an error (*DumpSave::bsnosym: "Table[..] is not defined as a symbol or a context"*) –  rainer Mar 15 '13 at 9:39
Hi rainer, you are fighting the syntax here: Subscript[vars,i] is not the easy way to do indexing in Mathematica. It is more of a typesetting convenience, that is, for display. The easy thing to do is to have vars be a list, and to index into that list. This is the way I answered the question below. –  bill s Mar 15 '13 at 10:00
@Mr. Wizard isn't that post about saving a whole function definition to a file? Whereas here I'm trying to save variable name based on subscript. I already read that post and found out about DumpSave. –  rainer Mar 15 '13 at 11:06

There are two easy ways I can think of to do this, but both require a change to your methods.

The first is to merely use indexed objects instead of Subscript, e.g. vars[1] = x; vars["thing"] = y; . . .. You would then use Save["file.m", vars].

The second will allow you to use Subscript but you will need to change the way the definitions are made, to attach the definitions to the symbol vars rather than Subscript. (Described here.)

vars /: Subscript[vars, n] = . . .


This creates UpValues on the symbol vars which will be saved by Save["file.m", vars].

A complete working example:

idx = Partition[RandomSample[Range[20], 20], 3];

Table[vars /: Subscript[vars, i] = Total[i], {i, idx}];

Save["vars-test.m", vars];


Now check what was saved:

FilePrint["vars-test.m"]


For additional (general) methods of saving definitions see this post.

-
@rainer I'm certainly glad we got that figured out. Thanks for the Accept. –  Mr.Wizard Mar 17 '13 at 13:18

Start with your data idx. What you are calculating is the Total of each of the triplets in idx. Let's do this the easy way and assign it to a variable

vars = Map[Total, idx]


You can save vars to a file like this:

DumpSave["test.mx", vars]


Then, after quitting and starting again, you can get the saved variables and data back by

<<test.x


Now, if you want the nth entry of vars, you type

 vars[[n]]

-
Thanks, but actually it's more difficult than that. I apply another more complicated function. And I do need this specific output because it's an input somewhere else –  rainer Mar 15 '13 at 10:12
You've seen the new integration with R? wolfram.com/mathematica/new-in-9/built-in-integration-with-r There's a whole page on how to translate data from Mathematica to R (and back): reference.wolfram.com/mathematica/RLink/tutorial/DataTypes.html –  bill s Mar 15 '13 at 10:19