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, 

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:

 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"

  • 1
    $\begingroup$ Does DumpSave[ ] not work properly for you? $\endgroup$
    – bill s
    Mar 15, 2013 at 8:22
  • 1
    $\begingroup$ @bills In the last sentence the OP said that he can't get the proper syntax for DumpSave[]. Can you help him? $\endgroup$ Mar 15, 2013 at 8:50
  • $\begingroup$ 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"*) $\endgroup$
    – rainer
    Mar 15, 2013 at 9:39
  • $\begingroup$ 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. $\endgroup$
    – bill s
    Mar 15, 2013 at 10:00
  • $\begingroup$ @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. $\endgroup$
    – rainer
    Mar 15, 2013 at 11:06

2 Answers 2


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:


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

  • $\begingroup$ @rainer I'm certainly glad we got that figured out. Thanks for the Accept. $\endgroup$
    – Mr.Wizard
    Mar 17, 2013 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


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


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.